Python calendar module | HTMLCalendar formatmonth() method

Calendar module allows to output calendars like program, and provides additional useful functions related to the calendar. Functions and classes defined in Calendar module use an idealized calendar, the current Gregorian calendar extended indefinitely in both directions.

class calendar.HTMLCalendar(firstweekday=0) can be used to generate generate HTML calendars.. formatmonth() method is one of the methods of HTMLCalendar instance.

formatmonth() method in Python is used to get month’s calendar as an HTML table.



Syntax: formatmonth(year, month, withyear=True)

Parameter:
year: year of the calendar
month: month of the calendar
withyear: [optional] If withyear is true the year will be included in the header, otherwise just the month name will be used.

Returns: Return a month’s calendar.

Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.HTMLCalendar(firstweekday = 0)
  
year = 2018
month = 9
# default value of width is 0
  
# printing formatmonth
print(text_cal.formatmonth(year, month))

chevron_right


Output:

filter_none

edit
close

play_arrow

link
brightness_4
code

<table border="0" cellpadding="0" cellspacing="0" class="month">
    <tr>
        <th colspan="7" class="month">September 2018</th>
    </tr>
    <tr>
        <th class="mon">Mon</th>
        <th class="tue">Tue</th>
        <th class="wed">Wed</th>
        <th class="thu">Thu</th>
        <th class="fri">Fri</th>
        <th class="sat">Sat</th>
        <th class="sun">Sun</th>
    </tr>
    <tr>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="sat">1</td>
        <td class="sun">2</td>
    </tr>
    <tr>
        <td class="mon">3</td>
        <td class="tue">4</td>
        <td class="wed">5</td>
        <td class="thu">6</td>
        <td class="fri">7</td>
        <td class="sat">8</td>
        <td class="sun">9</td>
    </tr>
    <tr>
        <td class="mon">10</td>
        <td class="tue">11</td>
        <td class="wed">12</td>
        <td class="thu">13</td>
        <td class="fri">14</td>
        <td class="sat">15</td>
        <td class="sun">16</td>
    </tr>
    <tr>
        <td class="mon">17</td>
        <td class="tue">18</td>
        <td class="wed">19</td>
        <td class="thu">20</td>
        <td class="fri">21</td>
        <td class="sat">22</td>
        <td class="sun">23</td>
    </tr>
    <tr>
        <td class="mon">24</td>
        <td class="tue">25</td>
        <td class="wed">26</td>
        <td class="thu">27</td>
        <td class="fri">28</td>
        <td class="sat">29</td>
        <td class="sun">30</td>
    </tr>
</table>

chevron_right


 

Code #2: With parameter width

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.HTMLCalendar(firstweekday = 0)
  
  
# printing formatmonth
print(text_cal.formatmonth(2018, 9))

chevron_right


Output:

filter_none

edit
close

play_arrow

link
brightness_4
code

<table border="0" cellpadding="0" cellspacing="0" class="month">
    <tr>
        <th colspan="7" class="month">September 2018</th>
    </tr>
    <tr>
        <th class="mon">Mon</th>
        <th class="tue">Tue</th>
        <th class="wed">Wed</th>
        <th class="thu">Thu</th>
        <th class="fri">Fri</th>
        <th class="sat">Sat</th>
        <th class="sun">Sun</th>
    </tr>
    <tr>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="noday"> </td>
        <td class="sat">1</td>
        <td class="sun">2</td>
    </tr>
    <tr>
        <td class="mon">3</td>
        <td class="tue">4</td>
        <td class="wed">5</td>
        <td class="thu">6</td>
        <td class="fri">7</td>
        <td class="sat">8</td>
        <td class="sun">9</td>
    </tr>
    <tr>
        <td class="mon">10</td>
        <td class="tue">11</td>
        <td class="wed">12</td>
        <td class="thu">13</td>
        <td class="fri">14</td>
        <td class="sat">15</td>
        <td class="sun">16</td>
    </tr>
    <tr>
        <td class="mon">17</td>
        <td class="tue">18</td>
        <td class="wed">19</td>
        <td class="thu">20</td>
        <td class="fri">21</td>
        <td class="sat">22</td>
        <td class="sun">23</td>
    </tr>
    <tr>
        <td class="mon">24</td>
        <td class="tue">25</td>
        <td class="wed">26</td>
        <td class="thu">27</td>
        <td class="fri">28</td>
        <td class="sat">29</td>
        <td class="sun">30</td>
    </tr>
</table>

chevron_right


 
Code #3: Year will be included in the header.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate working of formatmonth() method
  
# importing calendar module
import calendar
  
text_cal = calendar.HTMLCalendar(firstweekday = 0)
  
  
# printing formatmonth
print(text_cal.formatmonth(2018, 9, withyear = True))

chevron_right


Output:

filter_none

edit
close

play_arrow

link
brightness_4
code

<table border ="0" cellpadding ="0" cellspacing ="0" class ="month">
    <tr>
        <th colspan ="7" class ="month">September 2018</th>
    </tr>
    <tr>
        <th class ="mon">Mon</th>
        <th class ="tue">Tue</th>
        <th class ="wed">Wed</th>
        <th class ="thu">Thu</th>
        <th class ="fri">Fri</th>
        <th class ="sat">Sat</th>
        <th class ="sun">Sun</th>
    </tr>
    <tr>
        <td class ="noday"> </td>
        <td class ="noday"> </td>
        <td class ="noday"> </td>
        <td class ="noday"> </td>
        <td class ="noday"> </td>
        <td class ="sat">1</td>
        <td class ="sun">2</td>
    </tr>
    <tr>
        <td class ="mon">3</td>
        <td class ="tue">4</td>
        <td class ="wed">5</td>
        <td class ="thu">6</td>
        <td class ="fri">7</td>
        <td class ="sat">8</td>
        <td class ="sun">9</td>
    </tr>
    <tr>
        <td class ="mon">10</td>
        <td class ="tue">11</td>
        <td class ="wed">12</td>
        <td class ="thu">13</td>
        <td class ="fri">14</td>
        <td class ="sat">15</td>
        <td class ="sun">16</td>
    </tr>
    <tr>
        <td class ="mon">17</td>
        <td class ="tue">18</td>
        <td class ="wed">19</td>
        <td class ="thu">20</td>
        <td class ="fri">21</td>
        <td class ="sat">22</td>
        <td class ="sun">23</td>
    </tr>
    <tr>
        <td class ="mon">24</td>
        <td class ="tue">25</td>
        <td class ="wed">26</td>
        <td class ="thu">27</td>
        <td class ="fri">28</td>
        <td class ="sat">29</td>
        <td class ="sun">30</td>
    </tr>
</table>

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.