24
Python beginner Project: Calendar Builder

Hey, hi today we're gonna make a nice looking printable text file of monthly calendars for the month and year you desire.
Don't scare from dates and calendars, yes, they are some tricky topic in programming because there are so many different rules for condioning the number of the days in a month, which years are leap years, and which day of the week a particular date falls on.
Thankfully, Python's datetime module handles these details for us. We will generate a multiline string for the monthly calendar page.
Are you still with me, if yes, let's make this shit!!
Things you need to make This Calendar Builder are:
First steps:
Make a file
Now Code:
Make a file
Caledar_builder.py
Now Code:
import datetime
# Set up the constants:
DAYS = ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
'Friday', 'Saturday')
MONTHS = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
print('|| Calender-Builder ||')
After this
while True: # Loop to get a year from the user.
print('Enter the year for the calendar:')
response = input('> ')
if response.isdecimal() and int(response) > 0:
year = int(response)
break
print('Please enter a numeric year, like 2023.')
continue
while True: # Loop to get a month from the user.
print('Enter the month for the calendar, 1-12:')
response = input('> ')
if not response.isdecimal():
print('Please enter a numeric month, like 3 for March.')
continue
month = int(response)
if 1 <= month <= 12:
break
print('Please enter a number from 1 to 12.')
- print the message
The second loop:
After this
def getCalendarFor(year, month):
calText = ''
calText += (' ' * 34) + MONTHS[month - 1] + ' ' + str(year) + '\n'
calText += '...Sun.....Mon....Tue..Wed...Thur....Fri....Sat..\n'
weekSeparator = ('+----------' * 7) + '+\n'
blankRow = ('| ' * 7) + '|\n'
currentDate = datetime.date(year, month, 1)
while currentDate.weekday() != 6:
currentDate -= datetime.timedelta(days=1)
while True: # Loop over each week in the month.
calText += weekSeparator
dayNumberRow = ''
for i in range(7):
dayNumberLabel = str(currentDate.day).rjust(2)
dayNumberRow += '|' + dayNumberLabel + (' ' * 8)
currentDate += datetime.timedelta(days=1) # Go to next day.
dayNumberRow += '|\n' # Add the vertical line after Saturday.
# Add the day number row and 3 blank rows to the calendar text.
calText += dayNumberRow
for i in range(3): # (!) Try changing the 4 to a 5 or 10.
calText += blankRow
# Check if we're done with the month:
if currentDate.month != month:
break
# Add the horizontal line at the very bottom of the calendar.
calText += weekSeparator
return calText
calText = getCalendarFor(year, month)
print(calText) # Display the calendar.
Are you still with me this one will be a bit big!
- Try to change the abbreviations to SUN, MON and so forth to the week days if you want.
weekSeparator
The horizontal line string that separate weeks:blankRow
The blank rows have ten spaces in between the | day separators:while currentDate.weekday() != 6:
Get the first date in the month. (The datetime module handles all the complicated calendar stuff for us here.)
-
currentDate = datetime.date(year, month, 1)
Roll back currentDate until it is Sunday. (weekday() returns 6 for Sunday, not 0.)
dayNumberRow
is the row with the day number labels:for i in range(3):
(!) Try changing the 4 to a 5 or 10.calText = getCalendarFor(year, month)
print(calText)
# Display the calendar.
To save the printed code in a file
# Save the calendar to a text file:
calendarFilename = 'calendar_{}_{}.txt'.format(year, month)
with open(calendarFilename, 'w') as fileObj:
fileObj.write(calText)
print('Saved to ' + calendarFilename)
24