Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Python Calendar Module: Expert Guide with Code Examples

Python’s built‑in calendar module offers powerful tools for working with dates, months, and years. By leveraging classes like TextCalendar and HTMLCalendar, developers can create printable or web‑friendly calendars, iterate through days, and even compute custom scheduling rules.

Below is a step‑by‑step walkthrough of the most common use cases, complete with ready‑to‑run code snippets for both Python 2 and Python 3.

Step 1 – Generate a Text Calendar

Run the following script to see a plain‑text calendar for January 2025, starting the week on Thursday:

Python Calendar Module: Expert Guide with Code Examples

Change the start day to Sunday to verify the flexibility:

Python Calendar Module: Expert Guide with Code Examples

Step 2 – Render an HTML Calendar

For web applications, HTMLCalendar can generate a full HTML table that can be styled with CSS:

Python Calendar Module: Expert Guide with Code Examples

Step 3 – Iterate Over Days of a Month

The itermonthdays method yields all days in the month, including padding zeros for days that belong to adjacent months. This is useful for aligning calendars:

Python Calendar Module: Expert Guide with Code Examples

Step 4 – Retrieve Locale‑Aware Names

Python automatically honors the system’s locale for month and weekday names. Below is a sample output when printing calendar.month_name and calendar.day_name:

Python Calendar Module: Expert Guide with Code Examples

Locale settings vary by machine, so the results may differ for users in other countries.

Step 5 – Compute Custom Dates (e.g., First Monday of Each Month)

Below is a concise script that finds the first Monday of every month in 2025:

Python Calendar Module: Expert Guide with Code Examples

The logic uses calendar.monthcalendar to get a matrix of weeks, then checks the first two weeks for a non‑zero Monday value.

Python 2 Example

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
text = c.formatmonth(2025, 1)
print text

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
html = hc.formatmonth(2025, 1)
print html

# Loop over the days of a month
for i in c.itermonthdays(2025, 4):
    print i

# Locale‑aware names of months and days
for name in calendar.month_name:
    print name
for day in calendar.day_name:
    print day

# Find the first Monday of each month
for month in range(1, 13):
    mycal = calendar.monthcalendar(2025, month)
    week1 = mycal[0]
    week2 = mycal[1]
    if week1[calendar.MONDAY] != 0:
        auditday = week1[calendar.MONDAY]
    else:
        auditday = week2[calendar.MONDAY]
    print "%10s %2d" % (calendar.month_name[month], auditday)

Python 3 Example

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
text = c.formatmonth(2025, 1)
print(text)

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
html = hc.formatmonth(2025, 1)
print(html)

# Loop over the days of a month
for i in c.itermonthdays(2025, 4):
    print(i)

# Locale‑aware names of months and days
for name in calendar.month_name:
    print(name)
for day in calendar.day_name:
    print(day)

# Find the first Monday of each month
for month in range(1, 13):
    mycal = calendar.monthcalendar(2025, month)
    week1 = mycal[0]
    week2 = mycal[1]
    if week1[calendar.MONDAY] != 0:
        auditday = week1[calendar.MONDAY]
    else:
        auditday = week2[calendar.MONDAY]
    print("%10s %2d" % (calendar.month_name[month], auditday))

Takeaway

Python

  1. C# Windows Forms Development: A Hands‑On Tutorial
  2. Mastering Python’s strip() Method: Comprehensive Guide & Practical Examples
  3. Mastering Python’s Yield: Generator vs Return – A Practical Guide
  4. Python Counter in collections – Efficient Counting, Updating, and Arithmetic Operations
  5. Creating ZIP Archives in Python: From Full Directory to Custom File Selection
  6. Master Python Unit Testing with PyUnit: A Practical Guide & Example
  7. Python List index() – How to Find Element Positions with Practical Examples
  8. Master Python Regular Expressions: re.match(), re.search(), re.findall() – Practical Examples
  9. Master Python Multithreading: GIL Explained with Practical Examples
  10. Master Python Attrs: Build Advanced Data Classes with Practical Examples