Open In App

Python Program to Display Calendar of a Given Month

Displaying a calendar for a given month is a common task in programming, and Python provides several methods to achieve this. In this article, we will explore different approaches to creating a Python program that displays the calendar of a specified month. Whether using built-in libraries or third-party modules, these methods offer flexibility and ease of implementation for calendar generation.

Python Program To Display Calendar Of A Given Month

Below, are the methods of Python programs to Display the Calendar Of A Given Month in Python.



Python Program To Display Calendar Using calendar.month()

In this example, the below code utilizes the calendar.month() function from the built-in calendar module to generate a formatted calendar for the specified year and month. It validates the input month to ensure it falls within the valid range (1-12) and prints the calendar for the specified period, providing a clear and readable display of the month’s days and weeks.




import calendar
  
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))
  
if month < 1 or month > 12:
    print("Invalid month. Please enter a number between 1 and 12.")
else:
    cal = calendar.month(year, month)
    print("Calendar for {}-{}".format(year, month))
    print(cal)

Output



Enter the year: 2024
Enter the month (1-12): 2
Calendar for 2024-2
February 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

Python Program To Display Calendar Using calendar.HTMLCalendar()

In this example, below code leverages the calendar.HTMLCalendar class from the calendar module to create an HTML-formatted calendar with weeks starting on Sunday. After validating the input month, it generates the calendar using cal.formatmonth() and displays it using IPython’s display and HTML functions.




import calendar
from IPython.display import display, HTML
  
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))
  
if month < 1 or month > 12:
    print("Invalid month. Please enter a number between 1 and 12.")
else:
    cal = calendar.HTMLCalendar(calendar.SUNDAY)  # Start calendar weeks on Sunday
    print("Calendar for {}-{}".format(year, month))
    html_calendar = cal.formatmonth(year, month)
    display(HTML(html_calendar))

Output

Enter the year: 2021
Enter the month (1-12): 2
Calendar for 2021-2
February 2021
Sun Mon Tue Wed Thu Fri Sat
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

Python Program To Display Calendar Using calendar.monthcalendar()

In this example, below code employs the `calendar.monthcalendar()` function to generate a matrix representing the calendar for the specified year and month. The PrettyTable library is then utilized to create a formatted table with column headings for each day of the week. It populates the table with the days from the calendar matrix, handling empty slots with an empty string, resulting in a visually organized and readable representation.




import calendar
from prettytable import PrettyTable
  
year = int(input("Enter the year: "))
month = int(input("Enter the month (1-12): "))
  
if month < 1 or month > 12:
    print("Invalid month. Please enter a number between 1 and 12.")
else:
    cal = calendar.monthcalendar(year, month)
    print("Calendar for {}-{}".format(year, month))
    table = PrettyTable()
    table.field_names = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    for week in cal:
        table.add_row([day if day != 0 else "" for day in week])
    print(table)

Output

Enter the year: 2021
Enter the month (1-12): 7
Calendar for 2021-7
+-----+-----+-----+-----+-----+-----+-----+
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
+-----+-----+-----+-----+-----+-----+-----+
| | | | 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 | |
+-----+-----+-----+-----+-----+-----+-----+

Conclusion

In , conclusion , Python provides versatile methods for displaying calendars, ranging from simple textual output with calendar.month() to more visually appealing options like HTML output and tabular representation using libraries such as PrettyTable. With these tools, users can easily visualize and organize calendar data according to their specific needs and preferences.


Article Tags :