1. Home
  2. Computing & Technology
  3. Python

How To Create A HTML Calendar In Python Dynamically

By Al Lukaszewski, About.com

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.

8 of 10

Explore Python

More from About.com

  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

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

All rights reserved.