1. Home
  2. Computing & Technology
  3. Python

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.

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Python
  4. Web Development
  5. Python and HTML - HTML Calendars in Python - the Number of Weeks In A Month

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

All rights reserved.