1. Computing & Technology

How To Create A HTML Calendar In Python Dynamically

From , former About.com Guide

8 of 10

The Number of Weeks In A Month

Knowing the number of weeks in the month, we can create a for loop which counts through a range() from 0 to the number of weeks. As it does, it will print out the rest of the calendar.

 for w in range(0,nweeks): 
 week = month[w] 
 print "<tr>" 
 for x in xrange(0,7): 
 day = week[x] 
 if x == 5 or x == 6: 
 classtype = 'weekend' 
 else: 
 classtype = 'day' 
 
 if day == 0: 
 classtype = 'previous' 
 print '<td class="%s"></td>' %(classtype) 
 elif day == current_day: 
 print '<td class="%s"><strong>%s</strong></span><div class="%s"></div></td>' %(classtype, day, classtype) 
 else: 
 print '<td class="%s">%s</span><div class="%s"></div></td>' %(classtype, day, classtype) 
 print "</tr>" 
 
 print ''' </tbody> 
 </table> 
 </div> 
 </body> 
 </html>''' 

We will discuss this code line-by-line on the next page.

©2012 About.com. All rights reserved.

A part of The New York Times Company.