On this page
calendar.TextCalendar
Prev: calendar.setfirstweekday | Up: calendar | Next: calendar.timegm
Rel: calendar.calendar, calendar.Calendar, calendar.Day, calendar.firstweekday
Ref: docs.python.org — calendar.TextCalendar
Tags: #public #python3 #stdlib #calendar
Import
import calendar
from calendar import TextCalendar
Signature & Overview
calendar.TextCalendar(firstweekday=0)
Subclass of Calendar that outputs a calendar as a simple plain text similar to the UNIX program cal.
Methods & Attributes
TextCalendar.firstweekday(...)—TextCalendar.firstweekdaymethod/property.TextCalendar.formatday(self, day, weekday, width)— Returns a formatted day.TextCalendar.formatmonth(self, theyear, themonth, w=0, l=0)— Return a month's calendar string (multi-line).TextCalendar.formatmonthname(self, theyear, themonth, width, withyear=True)— Return a formatted month name.TextCalendar.formatweek(self, theweek, width)— Returns a single week in a string (no newline).TextCalendar.formatweekday(self, day, width)— Returns a formatted week day name.TextCalendar.formatweekheader(self, width)— Return a header for a week.TextCalendar.formatyear(self, theyear, w=2, l=1, c=6, m=3)— Returns a year's calendar as a multi-line string.TextCalendar.getfirstweekday(self)—TextCalendar.getfirstweekdaymethod/property.TextCalendar.itermonthdates(self, year, month)— Return an iterator for one month. The iterator will yield datetime.date values and will always iterate through complete weeks, so it will yield dates outside the specified month.TextCalendar.itermonthdays(self, year, month)— Like itermonthdates(), but will yield day numbers. For days outside the specified month the day number is 0.TextCalendar.itermonthdays2(self, year, month)— Like itermonthdates(), but will yield (day number, weekday number) tuples. For days outside the specified month the day number is 0.TextCalendar.itermonthdays3(self, year, month)— Like itermonthdates(), but will yield (year, month, day) tuples. Can be used for dates outside of datetime.date range.TextCalendar.itermonthdays4(self, year, month)— Like itermonthdates(), but will yield (year, month, day, day_of_week) tuples.TextCalendar.iterweekdays(self)— Return an iterator for one week of weekday numbers starting with the configured first one.TextCalendar.monthdatescalendar(self, year, month)— Return a matrix (list of lists) representing a month's calendar.TextCalendar.monthdays2calendar(self, year, month)— Return a matrix representing a month's calendar.TextCalendar.monthdayscalendar(self, year, month)— Return a matrix representing a month's calendar.TextCalendar.prmonth(self, theyear, themonth, w=0, l=0)— Print a month's calendar.TextCalendar.prweek(self, theweek, width)— Print a single week (no newline).TextCalendar.pryear(self, theyear, w=0, l=0, c=6, m=3)— Print a year's calendar.TextCalendar.setfirstweekday(self, firstweekday)—TextCalendar.setfirstweekdaymethod/property.TextCalendar.yeardatescalendar(self, year, width=3)— Return the data for the specified year ready for formatting. The return value is a list of month rows. Each month row contains up to width months.TextCalendar.yeardays2calendar(self, year, width=3)— Return the data for the specified year ready for formatting (similar to yeardatescalendar()). Entries in the week lists are (day number, weekday number) tuples. Day numbers outside this month areTextCalendar.yeardayscalendar(self, year, width=3)— Return the data for the specified year ready for formatting (similar to yeardatescalendar()). Entries in the week lists are day numbers.
Usage
import calendar
txt = calendar.TextCalendar(firstweekday=calendar.SUNDAY).formatmonth(2026, 12)
assert "December 2026" in txt and "Su Mo Tu We Th Fr Sa" in txt
Gotchas
- Cleanest way to format plain-text calendars without mutating global
firstweekday.
REPL Inspect
from calendar import TextCalendar
help(TextCalendar)
Linked from
Related notes
- tuts/python3/stdlib_module/calendar/setfirstweekday Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/timegm Linked here; links to this page; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/calendar Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/Calendar_class Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/Day Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/firstweekday Linked here; shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/calendar/HTMLCalendar shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/IllegalMonthError shared tags: #calendar, #stdlib
- tuts/python3/stdlib_module/calendar/IllegalWeekdayError shared tags: #calendar, #stdlib