1. Home
  2. Computing & Technology
  3. Python

How To Create A HTML Calendar In Python Dynamically

From , former About.com Guide

6 of 10

Printing the Days of the Week

Now that the basic layout is output, we can set up the calendar itself. A calendar, at its most basic point, is a table. So let's make a table in our HTML:

     print '''
     <table id="month" >
     <thead >
     <tr >
     <th class="weekend" >Sunday</th >
     <th >Monday</th >
     <th >Tuesday</th >
     <th >Wednesday</th >
    
     <th >Thursday</th >
     <th >Friday</th >
     <th class="weekend" >Saturday</th >
     </tr >
     </thead >
     <tbody >
     '''

Now our program will print our desired header with the current month and year. If you have used the command-line option mentioned earlier, here you should insert an if-else statement as follows:

if firstday == '0':      print '''
     <table id="month" >
     <thead >
     <tr >
     <th >Monday</th >
     <th >Tuesday</th >
     <th >Wednesday</th >
     <th >Thursday</th >
     <th >Friday</th >
     <th class="weekend" >Saturday</th >
     <th class="weekend" >Sunday</th >
     </tr >
     </thead >
     <tbody >
     '''
else: ## Here we assume a binary switch, a decision between '0' or not '0'; therefore, any non-zero argument will cause the calendar to start on Sunday.
     print '''
     <table id="month" >
     <thead >
     <tr >
     <th class="weekend" >Sunday</th >
     <th >Monday</th >
     <th >Tuesday</th >
     <th >Wednesday</th >
     <th >Thursday</th >
     <th >Friday</th >
     <th class="weekend" >Saturday</th >
     </tr >
     </thead >
     <tbody >
     '''

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 - Printing the Days of the Week

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

All rights reserved.