Header Ads

Header ADS

Python Calendar Module


Python Calendar

calendar module is a in built package which comes with python by default and it is used for calendar data formating and displaying.

This module allows you to output calendars & provides additional useful functions related to the calendar.



calendar module is very easy to use package in which only 3 lines of code returns a calendar.

In this Article we will use different sub-classes of calendar package to generate a calendar.



📓 Note : By default the calendar returned by python calendar module have Monday as the first day of week and Sunday as the last ( as according to the European  convention ).

but we can use setfirstweekday( ) to set the first of the week to sunday or to any other week day.





1. calendar.TextCalendar(firstweekday=0)

This class can be used to generate plain text calendars.

The Text Calendar instance have the following methods.


1.1  formatmonth( theyear , themonth ,w =0 , l =0 )

Return a month’s calendar in a multi-line string. If w is provided, it specifies the width of the date columns, which are centered. If l is given, it specifies the number of lines that each week will use. Depends on the first weekday as specified in the constructor or set by the setfirstweekday() method.

Code to illustrate :

       			 
#Dynamic Coding Calendar module
import calendar

c = calendar.TextCalendar()
c.setfirstweekday(0) # for monday
data = c.formatmonth(2020,7)

print(data)

 
        OUTPUT :





1.2  prmonth( theyear , themonth ,w =0 , l =0 )

Print a month’s calendar as returned by formatmonth().
        
Code to illustrate :
       			 
#Dynamic Coding Calendar module
import calendar

c = calendar.TextCalendar()
c.setfirstweekday(0) # for monday or use calendar.Monday instead of 0
data = c.prmonth(2020,7)

print(data)
 
 OUTPUT :





1.3 formatyear(theyearw=2l=1c=6m=3)

Return a m-column calendar for an entire year as a multi-line string. Optional parameters wl, and c are for date column width, lines per week, and number of spaces between month columns, respectively. Depends on the first weekday as specified in the constructor or set by the setfirstweekday() method. The earliest year for which a calendar can be generated is platform-dependent.


1.4 pryear(theyearw=2l=1c=6m=3)

Print the calendar for an entire year as returned by formatyear().





2. calendar.HTMLCalendar(firstweekday=0)

This class can be used to generate HTML calendars.

HTMLCalendar instances have the following methods:

2.1 formatmonth(theyearthemonthwithyear=True)

Return a month’s calendar as an HTML table. If withyear is true the year will be included in the header, otherwise just the month name will be used.

2.2 formatyear(theyearwidth=3)

Return a year’s calendar as an HTML table. width (defaulting to 3) specifies the number of months per row.

2.3 formatyearpage(theyearwidth=3css='calendar.css'encoding=None)

Return a year’s calendar as a complete HTML page. width (defaulting to 3) specifies the number of months per row. css is the name for the cascading style sheet to be used. None can be passed if no style sheet should be used. encoding specifies the encoding to be used for the output (defaulting to the system default encoding).


Code to illustrate :

       			 
#Dynamic Coding Calendar module
import calendar

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print(str)

 






For simple text calendars this module provides the following functions.

1. calendar.firstweekday()

Returns the current setting for the weekday to start each week.

2. calendar.isleap(year)

Returns True if year is a leap year, otherwise False.

3. calendar.leapdays(y1y2)

Returns the number of leap years in the range from y1 to y2 (exclusive), where y1 and y2 are years.

This function works for ranges spanning a century change.

4. calendar.weekday(yearmonthday)

Returns the day of the week (0 is Monday) for year (1970–…), month (112), day (131).

5. calendar.weekheader(n)

Return a header containing abbreviated weekday names. n specifies the width in characters for one weekday.

6. calendar.monthrange(yearmonth)

Returns weekday of first day of the month and number of days in month, for the specified year and month.

7. calendar.monthcalendar(yearmonth)

Returns a matrix representing a month’s calendar. Each row represents a week; days outside of the month a represented by zeros. Each week begins with Monday unless set by setfirstweekday().

8. calendar.prmonth(theyearthemonthw=0l=0)

Prints a month’s calendar as returned by month().

9. calendar.month(theyearthemonthw=0l=0)

Returns a month’s calendar in a multi-line string using the formatmonth() of the TextCalendar class.

10. calendar.prcal(yearw=0l=0c=6m=3)

Prints the calendar for an entire year as returned by calendar().

11. calendar.calendar(yearw=2l=1c=6m=3)

Returns a 3-column calendar for an entire year as a multi-line string using the formatyear() of the TextCalendar class.

12. calendar.timegm(tuple)

An unrelated but handy function that takes a time tuple such as returned by the gmtime() function in the time module, and returns the corresponding Unix timestamp value, assuming an epoch of 1970, and the POSIX encoding. In fact, time.gmtime() and timegm() are each others’ inverse.


Code to illustrate :

       			 
#Dynamic Coding Calendar module
import calendar

data = calendar.month(2020,7)
print(data)

 
 OUTPUT :




we know that we didn't code for every function and it is because if we code for every function available then the article becomes very big.
We hope you understand.
you can try yourself each function and if any error and doubts occur please let me know in comment section.
we will reply you as soon as possible.


No comments

Powered by Blogger.