Calendar Generator in Python
About the project:This project will allow you to generate and display a calendar for a specific month and year that you provide.
This script uses Python's built-in calendar module, which makes generating calendars.
How to use this Calendar Generator:
- Run the Python script.
- It will prompt you to enter the year (e.g., 2025).
- Then, it will ask for the month (1-12, e.g., 7 for July).
- The script will then display the calendar for that specific month and year.
- You can type q at any prompt to quit the program.
- After generating a calendar, it will also ask if you want to generate another.
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# calendar_generator.py
import calendar
def generate_calendar():
"""
Generates and displays a calendar for a user-specified month and year.
"""
print("--- Python Calendar Generator ---")
while True:
try:
year_input = input("Enter the year (e.g., 2024): ").strip()
# Check if the user wants to quit
if year_input.lower() == 'q':
print("Exiting Calendar Generator. Goodbye!")
return
year = int(year_input)
if not (1 <= year <= 9999): # Basic year range validation
print("Please enter a year between 1 and 9999.")
continue
month_input = input("Enter the month (1-12, e.g., 7 for July): ").strip()
# Check if the user wants to quit
if month_input.lower() == 'q':
print("Exiting Calendar Generator. Goodbye!")
return
month = int(month_input)
if not (1 <= month <= 12): # Month range validation
print("Please enter a month between 1 and 12.")
continue
# Create a TextCalendar instance
cal = calendar.TextCalendar(calendar.SUNDAY) # Week starts on Sunday
# Get the month's calendar as a multi-line string
# prcal(year) prints the entire year's calendar
# prmonth(year, month) prints a specific month's calendar
month_calendar_str = cal.prmonth(year, month)
print(f"\nCalendar for {calendar.month_name[month]} {year}:\n")
# prmonth already prints to stdout, so we just need to call it
# The previous line just generated the string for the print statement above
# The actual printing happens from cal.prmonth() or similar methods.
# To avoid double printing, let's refine this to directly print the calendar
# The calendar module's prmonth directly prints to console, so we don't need to capture and print.
# Alternative approach if you want to capture the string and then print:
import io
s = io.StringIO()
cal_instance = calendar.TextCalendar(calendar.SUNDAY)
cal_instance.prmonth(year, month, w=0, l=0, c=6, m=3, stream=s) # prmonth prints to stream
print(s.getvalue())
play_again = input("\nDo you want to generate another calendar? (yes/no): ").strip().lower()
if play_again != 'yes':
print("Exiting Calendar Generator. Goodbye!")
break
except ValueError:
print("Invalid input. Please enter a valid number for year and month, or 'q' to quit.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# This ensures that generate_calendar() is called only when the script is executed directly.
if __name__ == "__main__":
generate_calendar()