''' Python script to generate HTML for an empty calendar
The year number should be supplied as a command line parameter
Output should probably be redirected to a file, like this:
python calendar.py 2010 > 2010diary.html'''
import sys
from datetime import date, timedelta
# The resulting file can then be edited in any editor to add calendar entries
# dayoff class and holiday class can also be used to denote table cells
#
# This script was created by activityworkshop.net and is placed in the public domain.
def generate_year(year):
'''Outputs a html page containing an empty calendar for the given year'''
print(get_start_html(year))
print(get_year_head(year))
for month in range(12):
start_day = date(year, month+1, 1)
print("
%s
" % start_day.strftime("%B %Y"))
print(get_month_start())
print("")
num_blanks = start_day.isoweekday() - 1
for _ in range(num_blanks):
print(" | ")
curr_day = start_day
one_day = timedelta(days=1)
while curr_day.month == (month+1):
if curr_day.isoweekday() == 1 and curr_day.day > 1:
print("
")
style = " class='dayoff'" if curr_day.isoweekday() > 5 else ""
print("%d | " % curr_day.day)
curr_day += one_day
print("
")
print("")
print("
\n" + get_year_head(year, False))
print("\n" + get_end_html())
def get_start_html(year):
'''Returns the html for the start of the page, including styles'''
return ("\n\n\n" +
"" + str(year) + "\n" +
'''
''')
def get_end_html():
'''Returns the html for the end of the page'''
return "\n"
def get_year_head(year, show_year=True):
'''Returns the heading with links to previous and next years.
If show_year is true then it shows the current year too'''
html = ""
return html
def get_month_start():
'''Returns the table definition including day names on the top row'''
html = ""
test_date = date.today()
one_day = timedelta(days=1)
while test_date.isoweekday() > 1:
test_date = test_date + one_day
for _ in range(7):
html = html + "" + test_date.strftime("%a") + " | "
test_date = test_date + one_day
html = html + "
"
return html
# Use the given command line arguments to call generate_year
if __name__ == "__main__":
NUM_ARGS = len(sys.argv)
if NUM_ARGS == 1:
print("Calendar generator")
print("You need to give a year number too, like 'python calendar.py 2010'")
elif NUM_ARGS == 2:
try:
YEAR = int(sys.argv[1])
if YEAR < 50:
YEAR += 2000
if YEAR >= 1900 and YEAR <= 3000:
generate_year(YEAR)
else:
print("Calendar generator")
print("The year should be between 1900 and 3000")
except ValueError:
print("Calendar generator")
print("The year should be a number, not '%s'" % sys.argv[1])
else:
print("Calendar generator")
print("You gave more than one parameter, only a year number is required")