有没有一种简单的方法可以从Python中的ical文件中获取一天的事件?
对于非经常性的一天事件,我使用了类似的东西
from icalendar import Calendar
for event in Calendar.from_ical(ical).walk('vevent'):
if edate > ref_ref_day_start and event.get('dtstart').dt < ref_day_end:
# code here
但重复事件只在walk
中发生一次。
我可以看到,无限的事件迭代无休止地重复事件可能是一个问题。但肯定还有一种比自己计算重复次数更简单的方法,对吧?
(我找不到太多文档。我阅读了与重复事件相关的icalendar测试,但它似乎没有做任何类似的事情)。
您可以使用pyICSParser。
它将获取一个ICS文件,并在日期时间对象列表中返回重复事件的日期。
您需要指定希望返回这些事件的时间窗口,否则如果不使用UNTIL
或COUNT
参数重复出现的事件将呈现无限列表。
(免责声明我是该包的作者)
除了icalendar
之外,您还可以使用python递归事件。
pip install recurring-ical-events
然后,您可以将代码更改为
from icalendar import Calendar
import recurring_ical_events
import datetime
today = datetime.date.today()
for event in recurring_ical_events.of(Calendar.from_ical(ical)).at(today):
# code here for events of today
icalendar
只解析日历文件。此模块适用于事件重复。
是的。代替Ical Do it Bygcalcli
gcalcli是一个Python应用程序,允许您从命令行访问Google日历
[1]:https://github.com/insanum/gcalcli