Python Calendar Module : Python Tutorial 27

Python Calendar module :

Calendar” module in python is useful for displaying calendar and for doing other useful tasks related to the calendar . For using it, we need to import first “calendar” module.

Let’s try to print current month ( July, 2017 ) using python “calendar” module :

import calendar

year = 2017
month = 7

print (calendar.month(year,month))

It will print the below calendar view :

     July 2017
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

Instead of using “print”, we can also use calendar.prmonth(year,month) to print a calendar.

Change start day of Python calendar :

By default , these calendars have Monday as default starting date. We can use_** calendar.setfirstweekday(weekday)**_ to change it. “weekday” is in the range of 0 to 6 , 0 is Monday and 6 is Sunday.

import calendar

year = 2017
month = 7
calendar.setfirstweekday(calendar.SUNDAY)
calendar.prmonth(year,month)

It will print with Sunday as the first day :

     July 2017
Su Mo Tu We Th Fr Sa
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Printing calendar in HTML format :

Following program will print the full calendar for 2017 :

import calendar

year = 2017
col = 3
width = 2
lines = 1
space = 1

print calendar.TextCalendar(calendar.SUNDAY).formatyear(year, width, lines,space, col)

Here width is the date column width , lines is lines per week, space is number of spaces between month columns and col is number of columns for your calendar. Output :

                             2017

      January               February               March
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7            1  2  3  4            1  2  3  4
 8  9 10 11 12 13 14   5  6  7  8  9 10 11   5  6  7  8  9 10 11
15 16 17 18 19 20 21  12 13 14 15 16 17 18  12 13 14 15 16 17 18
22 23 24 25 26 27 28  19 20 21 22 23 24 25  19 20 21 22 23 24 25
29 30 31              26 27 28              26 27 28 29 30 31

       April                  May                   June
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
                   1      1  2  3  4  5  6               1  2  3
 2  3  4  5  6  7  8   7  8  9 10 11 12 13   4  5  6  7  8  9 10
 9 10 11 12 13 14 15  14 15 16 17 18 19 20  11 12 13 14 15 16 17
16 17 18 19 20 21 22  21 22 23 24 25 26 27  18 19 20 21 22 23 24
23 24 25 26 27 28 29  28 29 30 31           25 26 27 28 29 30
30

        July                 August              September
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
                   1         1  2  3  4  5                  1  2
 2  3  4  5  6  7  8   6  7  8  9 10 11 12   3  4  5  6  7  8  9
 9 10 11 12 13 14 15  13 14 15 16 17 18 19  10 11 12 13 14 15 16
16 17 18 19 20 21 22  20 21 22 23 24 25 26  17 18 19 20 21 22 23
23 24 25 26 27 28 29  27 28 29 30 31        24 25 26 27 28 29 30
30 31

      October               November              December
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7            1  2  3  4                  1  2
 8  9 10 11 12 13 14   5  6  7  8  9 10 11   3  4  5  6  7  8  9
15 16 17 18 19 20 21  12 13 14 15 16 17 18  10 11 12 13 14 15 16
22 23 24 25 26 27 28  19 20 21 22 23 24 25  17 18 19 20 21 22 23
29 30 31              26 27 28 29 30        24 25 26 27 28 29 30
                                            31

For more details on python calendar functions , check this link.

Similar tutorials :