简体   繁体   English

按时间顺序对日历事件进行排序

[英]Sorting icalendar events chronologically

I need to convert a large set of ics icalendar events into a LaTeX pgfgantt chart.我需要将大量 ics icalendar 事件转换为 LaTeX pgfgantt 图表。 For this I wrote a tiny script with the help of an answer found here .为此,我在此处找到的答案的帮助下编写了一个小脚本。

from icalendar import Calendar, Event
from datetime import datetime
from pytz import UTC # timezone

g = open('calendar.ics','rb')
gcal = Calendar.from_ical(g.read())
for component in gcal.walk():
    if component.name == "VEVENT":
        start = component.get('dtstart')
        end = component.get('dtend')
        print("\ganttbar{"+ str(component.get('summary')) + "}{" + str(start.dt) + "}{" + str(end.dt) + "}\\\\")
g.close()

The path has to be absolute for some reason.由于某种原因,路径必须是绝对的。

Let's add the data into pandas and try to sort them according to the start date.让我们将数据添加到 pandas 并尝试根据开始日期对其进行排序。

from icalendar import Calendar, Event
from datetime import datetime
from pytz import UTC # timezone
import pandas 

g = open('calendar.ics','rb')
gcal = Calendar.from_ical(g.read())
calevents = pandas.DataFrame(columns=['Event','Start','End',])
for component in gcal.walk():
    if component.name == "VEVENT":
        start = component.get('dtstart')
        end = component.get('dtend')
        calevents.loc[len(calevents)] = [component.get('summary'), start.dt, end.dt] 
g.close()
calevents['Start'] = pandas.to_datetime(calevents['Start'])
calevents = calevents.sort_values(by='Start', ignore_index=True)
for index in range(len(calevents)):
    print("\ganttbar{" + str(calevents.loc[index,"Event"]) + "}{" + str(calevents.loc[index,"Start"]) + "}{" + str(calevents.loc[index,"End"]) + "}\\\\")

Almost there, but the first date now contains a time.快到了,但第一个日期现在包含一个时间。 Can be removed in the output by removing ' 00:00:00'.可以通过删除“00:00:00”在 output 中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM