Write a Python program to create a HTML calendar with data for a specific year and month
The Python program prints a calendar for a specific month and year in HTML format. Here is a step-by-step explanation of the program:
- import calendar: This line imports the built-in calendar module, which provides useful functions related to the calendar.
- y = int(input("Enter Year :")) : This line prompts the user to enter the year for which they want to generate the calendar, and stores the user's input as an integer in the variable y.
- m = int(input("Enter Month of Number :")): This line prompts the user to enter the month number (1-12) for which they want to generate the calendar, and stores the user's input as an integer in the variable m.
- html_cal = calendar.HTMLCalendar(calendar.MONDAY): This line creates an instance of the HTMLCalendar class from the calendar module, with the argument calendar.MONDAY specifying that Monday should be the first day of the week in the generated calendar.
- print(html_cal.formatmonth(y, m)) : This line calls the formatmonth() method of the HTMLCalendar instance to generate an HTML calendar for the specified year and month, and prints the resulting HTML code to the console.
The output of the program will be an HTML calendar for the specified month and year, with each day of the month represented as a table cell in the calendar. The calendar will include the days of the week and the corresponding dates for the specified month and year, along with any remaining days from the previous or next month to fill out the calendar. The format of the calendar will be in HTML, with CSS styles for the formatting of the calendar.
Source Code
span style="color: #ff7700;font-weight:bold;">import calendar
y = int(input("Enter Year :"))
m = int(input("Enter Month of Number :"))
html_cal = calendar.HTMLCalendar(calendar.MONDAY)
print(html_cal.formatmonth(y, m))
Output