Python Glossary Русский

all Python 3calendarterm

calendar.HTMLCalendar

calendar classes

A calendar rendered as an HTML table: formatmonth() and formatyear() return markup with CSS classes for the weekdays.

Syntax

hc = calendar.HTMLCalendar(firstweekday=0)

The markup carries no styles — only classes (mon…sun, and noday for empty cells); styling comes from your own CSS or the cssclasses attributes. formatmonth() returns str, but formatyearpage() returns a whole page as bytes, which must be written in binary mode. The exact markup changes between versions (3.15 dropped border and cellpadding from <table>), so string-equality tests break.

Examples

import calendar
html = calendar.HTMLCalendar().formatmonth(2024, 2)
print(html.count('class="noday"'))  # → 6 — пустые клетки до 1-го и после 29-го
print('<td class="thu">1</td>' in html)  # → True
import calendar
page = calendar.HTMLCalendar().formatyearpage(2024)
print(type(page).__name__)  # → bytes
with open('year.html', 'wb') as f:
    f.write(page)

See also

Open in the full glossarydocs.python.orgReport an inaccuracy