-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendarMaker.py
100 lines (73 loc) · 3.3 KB
/
calendarMaker.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Calendar Maker, by Al sweigart [email protected]
Create monthly calendars, saved to textfile and fit for printing.
View this code at https://nostarch.com/big-book-small-python-projects
Tags: short"""
import datetime
# set up the constants:
DAYS = ('Sunday ','Monday','Tuesday', 'Wendnesday', 'Thursday ',
'Friday ', 'Saturday ')
MONTHS = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December')
print('Calendar Maker, by Alsweigart [email protected]')
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 year 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.')
def getCalendarfor(year,month):
calText = '' #calText eill contain the string of our calendar.
# Put the month and year at the top of the calendar:
calText += (' ' * 34) + MONTHS[month - 1] + ' ' + str(year) + '\n'
# Add the days of the week labels to the calendar:
# (!) Try changing this to abbreviations : SUN, MON, TUE, etc.
calText += '....Sun.....Mon....Tues...Wed...Thurs....Fri....Sat.. \n'
# The horizontal line string that seperate weeks:
weekSeperator = ('+----------' * 7) + '\n'
# The blank rows have ten spaces in between the | day sperators:
blankRow = ('| ' * 7) + '|\n'
# 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.)
while currentDate.weekday() != 6:
currentDate -= datetime.timedelta(days=1)
while True: # Loop over each week in the month.
calText += weekSeperator
# dayNumberRow is the row with the day number labels:
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 tex.
calText += dayNumberRow
for i in range(3): # (!) Try changing the 3 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 += weekSeperator
return calText
calText = getCalendarfor(year, month)
print(calText) #Disply the calendar
# 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)