Python has an built-in module named Calendar
that contains useful classes and functions to support a variety of calendar operations. By default, the Calendar
module follows the Gregorian calendar, where Monday is the first day (0) of the week and Sunday is the last day of the week (6).
In Python, datetime and time modules also provide low-level calendar-related functionalities. In addition to these modules, the Calendar
module provides essential functions related to displaying and manipulating calendars.
To print and manipulate calendars, the Calendar
module has 3 important classes: Calendar
, TextCalendar
, and HTMLCalendar
. In this article, we will see how these classes can help implement a variety of calendar related functions.
To use the Calendar
module, we need to first import the module using:
import calendar
Let's take a look at the list of useful functions in this module.
class calendar.Calendar :
Calendar class creates a Calendar object. A Calendar object provides several methods that can be used for preparing the calendar data for formatting. This class doesn%u2019t do any formatting itself. This is the job of subclasses. Calendar class allows the calculations for various task based on date, month, and year. Calendar class provides the following methods:
FUNCTION | DESCRIPTION |
---|---|
iterweekdays() | Returns an iterator for the week day numbers that will be used for one week |
itermonthdates() | Returns an iterator for the month (1%u201312) in the year |
itermonthdays() | Returns an iterator of a specified month and a year |
itermonthdays2() | Method is used to get an iterator for the month in the year similar to itermonthdates(). Days returned will be tuples consisting of a day of the month number and a week day number. |
itermonthdays3() | Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month and a day of the month numbers. |
itermonthdays4() | Returns an iterator for the month in the year similar to itermonthdates(), but not restricted by the datetime.date range. Days returned will be tuples consisting of a year, a month, a day of the month, and a day of the week numbers. |
monthdatescalendar() | Used to get a list of the weeks in the month of the year as full weeks |
monthdays2calendar() | Used to get a list of the weeks in the month of the year as full weeks |
monthdayscalendar | Used to get a list of the weeks in the month of the year as full weeks |
yeardatescalendar() | Used to get a list of the weeks in the month of the year as full weeks |
yeardays2calendar() | Used to get the data for specified year. Entries in the week lists are tuples of day numbers and weekday numbers |
yeardayscalendar() | Used to get the data for specified year. Entries in the week lists are day numbers |
class calendar.TextCalendar :
TextCalendar class can be used to generate plain text calendars. TextCalendar class in Python allows you to edit the calendar and use as per your requirement.
FUNCTION | DESCRIPTION |
---|---|
formatmonth() | Method is used to get month%u2019s calendar in a multi-line string |
prmonth() | Method is used to print a month%u2019s calendar as returned by formatmonth() |
formatyear() | Method is used to get m-column calendar for an entire year as a multi-line string |
pryear() | Method is used to print the calendar for an entire year as returned by formatmonth() |
class calendar.HTMLCalendar :
HTMLCalendar class can be used to generate HTML calendars. HTMLCalendar class in Python allows you to edit the calendar and use as per your requirement.
FUNCTION | DESCRIPTION |
---|---|
formatmonth() | Method is used to get month%u2019s calendar as an HTML table |
formatyear() | Method is used to get year%u2019s calendar as an HTML table. |
formatyearpage() | Method is used to get year%u2019s calendar as a complete HTML page |
Simple TextCalendar class :
For simple text calendars calendar module provides the following functions :
FUNCTION | DESCRIPTION |
---|---|
setfirstweekday() | Function sets the day start number of week |
firstweekday() | Function returns the first week day number. By default 0 (Monday) |
isleap() | Function checks if year mentioned in argument is leap or not |
leapdays() | Function returns the number of leap days between the specified years in arguments |
weekday() | Function returns the week day number(0 is Monday) of the date specified in its arguments |
weekheader() | Returns a header containing abbreviated weekday names |
monthrange() | Function returns two integers, first, the starting day number of week(0 as monday), second, the number of days in the month |
monthcalendar() | Returns a matrix representing a month%u2019s calendar. Each row represents a week; days outside of the month are represented by zeros |
prmonth() | Function also prints the month of specific year but there is no need of %u201Cprint%u201D operation to execute this |
month() | Function prints the month of a specific year mentioned in arguments |
prcal() | Function also prints the calendar of specific year but there is no need of %u201Cprint%u201D operation to execute this |
calendar() | Function displays the year, width of characters, no. of lines per week and column separations. |
Comments