日历文件:将csv文件转换为ics

日历文件:将csv文件转换为ics

# pip install icalendar

# %%
import pandas as pd

df = pd.read_csv('1.csv',  
                encoding='gbk',
                header=0, 
                parse_dates=[0,1], 
                date_parser=lambda x: pd.to_datetime(x, format='%Y/%m/%d %H:%M'))

print(df)


# %%
from icalendar import Calendar, Event

cal = Calendar()
cal.add('version', '2.0')


for row in df.itertuples(index=False):
    stime, etime, thing = row[0], row[1], row[2]

    # print(row[0], row[1], row[2])

    event = Event()
    event.add('summary', thing)
    event.add('dtstart', stime)
    event.add('dtend', etime)
    # print(event)

    cal.add_component(event)

    txt = cal.to_ical()
    print(str(txt, encoding='utf8'))


# %%
with open('1.ics', 'wb') as f:
    f.write(txt)

你可能感兴趣的:(日历文件:将csv文件转换为ics)