everyday

Python - Calendar Module - HTML Calendar

One of more interesting features of calendar module, is the HTML calendar which does all the hard job of adding tags and classes to each date of the month and present the result in clean table format.

We can use those classes to add more styles our calendar. Below sample which also shows the text calendar which can be used for various cases.

import calendar

c = calendar.TextCalendar(calendar.MONDAY)
html_calendar = calendar.HTMLCalendar(calendar.SUNDAY)
c.prmonth(2022,2)
print(html_calendar.formatmonth(2020,2))
February 2022
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

<table border="0" cellpadding="0" cellspacing="0" class="month">
<tr><th colspan="7" class="month">February 2020</th></tr>
<tr><th class="sun">Sun</th><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></tr>
<tr><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td><td class="sat">1</td></tr>
<tr><td class="sun">2</td><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></tr>
<tr><td class="sun">9</td><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></tr>
<tr><td class="sun">16</td><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></tr>
<tr><td class="sun">23</td><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></tr>
</table>

This is part of series of articles from Python Module of the Week

#pymotw

- 1 toast