generating an ics file from a tab delimited table

This commit is contained in:
cqql 2024-11-18 13:01:52 +01:00
commit 97c23f84bf
2 changed files with 65 additions and 0 deletions

1
README.md Normal file
View file

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

64
parse.py Normal file
View file

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