Monthly Archives: June 2014

Customizing HTMLCalendar class in Python


Recently I had a requirement of generating HTML calendar of a selected year in Python (with Django).  The first choice was very obvious, the easiest way of generating an HTML calendar in python is using the HTMLCalendar in built-in calendar module. However, this class returns the Calendar view in a simple string format. I needed a bit more. I needed the date objects instead of simple numbers in table cells and perform some custom logic based on those values. Below is how I implemented the feature, by overriding the HTMLCalendar class.

The above defined classes and variables are available in calendar module and can be imported instead of rewriting here. I’ve written them for the reference purpose only.

Below is a custom class inheriting HTMLCalendar class: