From 97c23f84bfb19c4a41ea18d267fb65c44afb17cf Mon Sep 17 00:00:00 2001 From: cqql Date: Mon, 18 Nov 2024 13:01:52 +0100 Subject: [PATCH] generating an ics file from a tab delimited table --- README.md | 1 + parse.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 README.md create mode 100644 parse.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..14357ce --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +I wanted to generate a big ical file with recurring events for all birthdays in my family from a spreadsheet we put together, so I wrote this script. No python library worked well for this, so I had to generate the ical manually. \ No newline at end of file diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..6f8904e --- /dev/null +++ b/parse.py @@ -0,0 +1,64 @@ +import pytz +from datetime import datetime, timedelta, date + +# copied from a spreadsheet (so it's tab separated) +bdays = """ + +lastname firstname 07-May +lastname firstname 07-May +lastname firstname 07-May +lastname firstname 07-May +lastname firstname 07-May +lastname firstname 07-May + + +""".strip().split('\n') + +months = [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" +] + +months = {m : i for i, m in enumerate(months)} + +bdays = [[cell.strip() for cell in line.split("\t")] for line in bdays] + +tzname = "Europe/Warsaw" +tz = pytz.timezone(tzname) + + +cal = """ +BEGIN:VCALENDAR +VERSION:2.0 +PRODID:Birthdays autogenerated +""".strip() + +for sname, fname, bday_date in bdays: + event = "BEGIN:VEVENT" + day, month = bday_date.split('-') + print(f'{sname:<16}|{fname:<16}|{day:>02}.{months[month]+1:>02}') + + event += "\nUID:" + str((fname+sname+bday_date).__hash__()) + event += "\nDTSTAMP;TZID=" + tzname + ":" + datetime.now(tz=tz).strftime("%Y%m%dT%H%M%S") + event += "\nDTSTART;VALUE=DATE:" + f'{datetime.now().year}{months[month]+1:>02}{day:>02}' + # event += "\n" + event += "\nSUMMARY:" + f'🎂 {fname} {sname} birthday!' + event += "\nRRULE:FREQ=YEARLY" + event += "\nEND:VEVENT" + print(event) + cal += "\n" + event + +cal += "\nEND:VCALENDAR" + +with open("birthday_calendar.ical", 'w', encoding='utf-8') as f: + f.write(cal) \ No newline at end of file