python calendar.HTMLCalendar

前端 未结 5 1018
面向向阳花
面向向阳花 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: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
    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(" "," ")
    
    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.

提交回复
热议问题