Python Program to Print a Calendar

Python provides a powerful built-in module called calendar that allows developers to work with calendars efficiently. Whether you’re building a scheduling app or just exploring Python, printing a calendar is a fun and practical task.


Python Code: Printing a Calendar

Here’s a simple Python program to display a calendar for a specific month and year.

# Import the calendar module
import calendar

# Input the year and month
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))

# Display the calendar for the given month and year
print(calendar.month(year, month))

How the Code Works

  1. Import the calendar Module:
    • The calendar module provides functions to generate and work with calendars.
  2. Input Year and Month:
    • The program prompts the user to input the desired year and month.
  3. Generate the Calendar:
    • The calendar.month(year, month) function generates a formatted string representing the calendar for the specified month and year.

Sample Output

If you input year = 2024 and month = 12, the program will output:

   December 2024
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 29
30 31

Enhanced Calendar Features

The calendar module offers several other functionalities that can enhance your program. Let’s look at some advanced features:

1. Print the Calendar for an Entire Year

To display the full calendar for a given year, use the calendar.prcal() function:

# Input the year
year = int(input("Enter the year: "))

# Print the entire calendar for the year
print(calendar.TextCalendar().formatyear(year))

2. Display Calendar in HTML Format

You can create an HTML version of the calendar using HTMLCalendar:

# Import the HTMLCalendar class
from calendar import HTMLCalendar

# Input the year and month
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))

# Create an HTMLCalendar object
cal = HTMLCalendar()

# Generate the HTML calendar
print(cal.formatmonth(year, month))

Customizing the Calendar Display

Here are some options to customize the calendar:

  1. First Day of the Week:
    • By default, Monday is the first day of the week.
    • You can change this with: calendar.setfirstweekday(calendar.SUNDAY)
  2. Locale Support:
    • Display the month names and weekdays in different languages: import locale locale.setlocale(locale.LC_TIME, 'es_ES') # For Spanish print(calendar.month(2024, 12))
  3. Highlight Specific Dates:
    • You can overlay custom styles (if using HTMLCalendar) or add markers in text-based calendars.

Applications of Calendar in Python

  • Date-based Scheduling: Automate meeting schedules or booking systems.
  • Event Management: Highlight specific dates for holidays, events, or deadlines.
  • Interactive UIs: Generate calendars dynamically in web or desktop applications.

Conclusion

Python’s calendar module makes it easy to work with dates and generate formatted calendars. From simple month displays to advanced features like HTML calendars, you have endless possibilities to integrate calendars into your projects.

Would you like me to help with more advanced calendar functionalities or a specific use case?


Share with our team

Leave a Comment