The calendar module in the standard library provide classes for working with dates. It allows us to format dates in a variety of ways, perform calculations on dates, determine relationships between dates, etc.

The HTMLCalendar  class generates a graphical representation of a calendar in HTML format. We can use this class to create and customize our own calendars in a web page.

Generate a html calendar for a certain month.

import calendar

html_cal = calendar.HTMLCalendar(firstweekday=0)
year = 2023
month = 8
print(html_cal.formatmonth(year, month))

The formatmonth() method  generates a html table with the values of the given month in a given year.

The result of the returned html is as follows without any styling:

August 2023
Mon Tue Wed Thu Fri Sat Sun
  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      

We can format the html calendar to look more visually appealing by applying custom styling.

August 2023
Mon Tue Wed Thu Fri Sat Sun
  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      

Html calendar for the current month.

import calendar
from datetime import date

html_cal = calendar.HTMLCalendar(firstweekday=0)

today = date.today()
year = today.year
month = today.month

print(html_cal.formatmonth(year, month))
August 2023
Mon Tue Wed Thu Fri Sat Sun
  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      

Html calendar for the whole year

We can use formatyear() method to generate a HTML table representing a calendar for a given year.

import calendar
from datetime import date

html_cal = calendar.HTMLCalendar(firstweekday=0)

today = date.today()
year = today.year

print(html_cal.formatyear(year)[0:600], '.............')
2023
January
Mon Tue Wed Thu Fri Sat Sun
            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          
February
Mon Tue Wed Thu Fri Sat Sun
    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          
March
Mon Tue Wed Thu Fri Sat Sun
    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    
April
Mon Tue Wed Thu Fri Sat Sun
          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
May
Mon Tue Wed Thu Fri Sat Sun
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        
June
Mon Tue Wed Thu Fri Sat Sun
      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    
July
Mon Tue Wed Thu Fri Sat Sun
          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            
August
Mon Tue Wed Thu Fri Sat Sun
  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      
September
Mon Tue Wed Thu Fri Sat Sun
        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  
October
Mon Tue Wed Thu Fri Sat Sun
            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          
November
Mon Tue Wed Thu Fri Sat Sun
    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      
December
Mon Tue Wed Thu Fri Sat Sun
        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

Html calendar as a page

In the previous examples, the methods returned the calendar as a HTML table. The formatyearpage() formats the calendar  as a complete html page.

import calendar
from datetime import date

html_cal = calendar.HTMLCalendar(firstweekday=0)

today = date.today()
year = today.year

print(html_cal.formatyearpage(year)[0:600], '.............')