python calendar.HTMLCalendar

前端 未结 5 1019
面向向阳花
面向向阳花 2021-02-02 04:10

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 l

相关标签:
5条回答
  • 2021-02-02 04:35

    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. ;)

    0 讨论(0)
  • 2021-02-02 04:37

    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.

    0 讨论(0)
  • 2021-02-02 04:37

    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.

    0 讨论(0)
  • 2021-02-02 04:40

    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.

    0 讨论(0)
  • 2021-02-02 04:42

    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)
    
    0 讨论(0)
提交回复
热议问题