1. Home
  2. Computing & Technology
  3. Python

How To Create A HTML Calendar In Python Dynamically

From Al Lukaszewski, for About.com

1 of 10

Introduction

Python's calendar module is part of the standard library. It allows the output of a calendar by month or by year and also provides other, calendar-related functionality.

The calendar module itself depends on the datetime module. But we will also need datetime for our own purposes, as we will see. So we should import both of these. Also, in order to do some string splitting, we will need the re module. Let's import them all in one go.

import re, datetime, calendar

By default, the calendars begin the week with Monday (day 0), per the European convention, and ends with Sunday (day 6). If you prefer Sunday as the first day of the week, use the setfirstweekday() method to change the default to day 6 as follows:

calendar.setfirstweekday(6)

To toggle between the two, you could pass the first day of the week as an argument using the sys module. You would then check the value with an if statement and set the setfirstweekday() method accordingly.

import sys
firstday = sys.argv[1]
if firstday == "6":
    calendar.setfirstweekday(6)

Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Web Development
  5. Python and HTML - HTML Calendars in Python - How To Create A HTML Calendar In Python Dynamically

©2009 About.com, a part of The New York Times Company.

All rights reserved.