python calendar.HTMLCalendar

Question:

I think I want to use pythons built in calendar module to create an HTML calendar with data. I say I think because I’ll probably think of a better way, but right now it’s a little personal. I don’t know if this was intended to be used this way but it seems like it is a little pointless if you can’t at least making the days into a <a hrefs>.

This sets up a calendar for this month with Sunday as the first day.

import calendar
myCal = calendar.HTMLCalendar(calendar.SUNDAY)
print myCal.formatmonth(2009, 7)

it prints

<table border="0" cellpadding="0" cellspacing="0" class="month">n<tr>
<th colspan="7" class="month">July 2009</th></tr>n<tr><th class="sun">Sun</th>
<th class="mon">Mon</th><th class="tue">Tue</th><th class="wed">Wed</th>
<th class="thu">Thu</th><th class="fri">Fri</th><th class="sat">Sat</th></tr>n
<tr><td class="noday">&nbsp;</td><td class="noday">&nbsp;</td>
<td class="noday">&nbsp;</td><td class="wed">1</td><td class="thu">2</td><td class="fri">3</td>
<td class="sat">4</td></tr>n<tr><td class="sun">5</td><td class="mon">6</td><td class="tue">7</td>
<td class="wed">8</td><td class="thu">9</td><td class="fri">10</td>
<td class="sat">11</td></tr>n<tr><td class="sun">12</td><td class="mon">13</td>
<td class="tue">14</td><td class="wed">15</td><td class="thu">16</td><td class="fri">17</td>
<td class="sat">18</td></tr>n<tr><td class="sun">19</td><td class="mon">20</td>
<td class="tue">21</td><td class="wed">22</td><td class="thu">23</td><td class="fri">24</td>
<td class="sat">25</td></tr>n<tr><td class="sun">26</td><td class="mon">27</td>
<td class="tue">28</td><td class="wed">29</td><td class="thu">30</td><td class="fri">31</td>
<td class="noday">&nbsp;</td></tr>n</table>n

I would like to insert some data into the HTMLCalendar object before it renders the html string. I just can’t figure out how.

For example

<td class="tue">28<br />[my data]</td>
Asked By: Kenny Pyatt

||

Answers:

It’s hard to say without knowing exactly what you’re trying to accomplish, but here’s one idea.

Instead of printing myCal.formatmonth(2009, 7), why don’t you assign it to a string. Then you could manipulate it, perhaps with a regex.

Here’s a really bad example:

import calendar
import re

myCal = calendar.HTMLCalendar(calendar.SUNDAY)
myStr = myCal.formatmonth(2009, 7)

re.sub('28', '28<br/>[My Data]', myStr)

print myStr

It does what you want, but it’s pretty ugly.

Answered By: Anthony Lewis

You may load html, which seems to be valid XMl into a XML tree, modify it and again output it.
e.g. this adds <br/> cool to each Tue td node.

import calendar
import xml.etree.ElementTree as etree

myCal = calendar.HTMLCalendar(calendar.SUNDAY)
htmlStr = myCal.formatmonth(2009, 7)
htmlStr = htmlStr.replace("&nbsp;"," ")

root = etree.fromstring(htmlStr)
for elem in root.findall("*//td"):
    if elem.get("class") != "tue":
        continue
    elem.text += "!"

    br = etree.SubElement(elem, "br")
    br.tail = "cool!"

print etree.tostring(root)

I do not yet know why you need to generate a HTML calendar, but there are better ways of doing that depending on needs and framework you are using.

Answered By: Anurag Uniyal

The calendar module has usually been pretty useless, but in 2.5 it introduced the Calendar object. This won’t render an HTML calendar for you, but it has loads of methods that will help you render a calendar.

For example, monthdatescalendar(year, month) will give you a list of all weeks in the month given, where each week in turn is a list of the seven days. So monthdatescalendar(2009,7) will start with [[datetime.date(2009, 6, 29), datetime.date(2009, 6, 30), and end with datetime.date(2009, 8, 1), datetime.date(2009, 8, 2)]]

With this, it then becomes a trivial exercise to generate the HTML you want.

Answered By: Lennart Regebro

Create a new class inheriting from HTMLCalendar.
Override the formatday method.

Whoever makes comments like “this library is useless” obviously doesn’t understand Python.

class EmployeeScheduleCalendar(HTMLCalendar):
    def formatday(self, day, weekday):
        """
          Return a day as a table cell.
        """
        if day == 0:
            return '<td class="noday">&nbsp;</td>' # day outside month
        else:
            return '<td class="%s"><a href="%s">%d</a></td>' % (self.cssclasses[weekday], weekday, day)
Answered By: Alan Hynes

You can subclass HTMLCalendar and override its methods in order to do whatever you want, like so: http://journal.uggedal.com/creating-a-flexible-monthly-calendar-in-django/

If you refer to the documentation for HTMLCalendar (http://svn.python.org/view/python/branches/release27-maint/Lib/calendar.py?view=markup), you’ll see that the formatday() method is very straightforward. Simply override it, as in the example linked above, to do whatever you’d like. So HTMLCalendar isn’t so pointless after all. 😉

Answered By: Lockjaw

You can create an HTMLcalendar object and then assign the class using the cssclasses attribute, later you just have to create the calendar using the formatmonth method, this is how I did it for a django app:

from calendar import HTMLCalendar

today = datetime.today()
    month = today.month
    year = today.year
    cal = HTMLCalendar()
    cal.cssclasses = ["mon text-bold", "tue", "wed", "thu", "fri", "sat", "sun red"]
    final_cal = cal.formatmonth(year, month)

This is my source: https://docs.python.org/3/library/calendar.html

Answered By: Rodrigo Reyes
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.