python python日历.HTMLCalendar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1101524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:29:20  来源:igfitidea点击:

python calendar.HTMLCalendar

pythonhtmlcalendar

提问by Kenny Pyatt

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

我想我想使用日历模块中内置的pythons来创建一个带有数据的HTML日历。我说我认为是因为我可能会想到更好的方法,但现在这有点个人化。我不知道这是否打算以这种方式使用,但如果您至少不能将这些日子变成<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.

我想在呈现 html 字符串之前将一些数据插入到 HTMLCalendar 对象中。我就是不知道怎么做。

For example

例如

<td class="tue">28<br />[my data]</td>

采纳答案by Anurag Uniyal

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/> coolto each Tue td node.

您可以将似乎是有效 XMl 的 html 加载到 XML 树中,对其进行修改并再次输出。例如,这会添加<br/> cool到每个 Tue td 节点。

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.

我还不知道您为什么需要生成 HTML 日历,但是根据您使用的需求和框架,有更好的方法可以做到这一点。

回答by Alan Hynes

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

创建一个继承自 HTMLCalendar 的新类。覆盖 formatday 方法。

Whoever makes comments like "this library is useless" obviously doesn't understand Python.

发表“这个库没用”之类的评论的人显然不了解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)

回答by Lennart Regebro

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.

日历模块通常没什么用,但在 2.5 中它引入了 Calendar 对象。这不会为您呈现 HTML 日历,但它有很多方法可以帮助您呈现日历。

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)]]

例如,monthdatescalendar(year,month) 将为您提供给定月份中所有周的列表,其中每周依次是 7 天的列表。所以 monthdatescalendar(2009,7) 将开始[[datetime.date(2009, 6, 29), datetime.date(2009, 6, 30),和结束datetime.date(2009, 8, 1), datetime.date(2009, 8, 2)]]

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

有了这个,生成你想要的 HTML 就变成了一个微不足道的练习。

回答by Lockjaw

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/

您可以继承 HTMLCalendar 并覆盖其方法以执行您想做的任何事情,如下所示: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. ;)

如果您参考 HTMLCalendar 的文档 (http://svn.python.org/view/python/branches/release27-maint/Lib/calendar.py?view=markup),您将看到 formatday() 方法非常简单。只需覆盖它,如上面链接的示例中所示,即可执行您想做的任何操作。所以 HTMLCalendar 毕竟不是那么毫无意义。;)

回答by Anthony Lewis

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.

与其打印 myCal.formatmonth(2009, 7),不如将它分配给一个字符串。然后你可以操纵它,也许用正则表达式。

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.

它做你想做的,但它很丑陋。