Kjetil's Information Center: A Blog About My Projects

HTML Calendar Generator

There is probably a ton of these already, but here is my contribution, based on my personal preferences. I prefer to have week numbers on my calendars, and that the week starts on Mondays. The output is in a 4x3 month format, suitable for printing on a A4 sized landscape oriented paper.

Here is the Python script:

#!/usr/bin/python

import calendar
import datetime
import sys

if len(sys.argv) > 1:
    try:
        year = int(sys.argv[1])
    except ValueError:
        sys.exit(1)
else:
    year = datetime.date.today().year

print '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>%s</title>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
  <style type="text/css">
    table.year { 
      background-color: white;
    }
    table.month {
      background-color: gray;
      border: 5px solid white
    }
    td.year-name {
      background-color: white;
      text-align: center;
      font-weight: bold;
    }
    td.month-name {
      background-color: white;
      text-align: center;
      font-weight: bold;
      height: 20px;
    }
    td.wday {
      background-color: lightgray;
      color: black;
      text-align: center;
      width: 30px;
      height: 20px;
    }
    td.day {
      background-color: white;
      color: black;
      text-align: center;
      width: 30px;
      height: 20px;
    }
    td.week {
      background-color: lightgray;
      color: black;
      text-align: center;
      font-weight: bold;
      width: 25px;
      height: 20px;
    }
    h1 {
      text-align: center;
    }
  </style>
</head>
<body>
<table class="year">
<tr><td class="year-name" colspan="4"><h1>%s</h1></td></tr>
<tr>
<td>''' % (year, year)

for month in range(1, 13):
    skip_days, last_day = calendar.monthrange(year, month)
    day = 1

    print '<table class="month">'
    print '<tr><td class="month-name" colspan="8">%s</td></tr>''' % (calendar.month_name[month])

    print '<tr>'
    print '<td></td>'
    for wday in range(0, 7):
        print '<td class="wday">%s</td>' % (calendar.day_abbr[wday])
    print '</tr>'

    for row in range(6):
        print '<tr>'
        try:
            week = datetime.date(year, month, day).isocalendar()[1]
            print '<td class="week">%d</td>' % (week)
        except ValueError:
            print '<td class="week"></td>'

        for col in range(7):
            if skip_days:
                print '<td class="day"></td>'
                skip_days -= 1
            else:
                if day > last_day:
                    print '<td class="day"></td>'
                else:
                    print '<td class="day">%d</td>' % (day)
                    day += 1

        print '</tr>'

    print '</table>'
    if month % 4 == 0:
        print '</td>'
        print '</tr>'
        if month != 12:
            print '<tr>'
            print '<td>'
    else:
        print '</td>'
        print '<td>'

print '</table>'
print '</body>'
print '</html>'
          


Here is a calendar generated for 2014.

Topic: Scripts and Code, by Kjetil @ 01/02-2014, Article Link