Skip to content

Instantly share code, notes, and snippets.

@neaxi
Last active October 2, 2023 15:01
Show Gist options
  • Save neaxi/a3e7b1bbbe7ac945b31eb0aea7d688f0 to your computer and use it in GitHub Desktop.
Save neaxi/a3e7b1bbbe7ac945b31eb0aea7d688f0 to your computer and use it in GitHub Desktop.
.csv to .ics (iCal) birthday event generator
'''
What:
Creates an .ics file with iCal events based on birthdays loaded
from a .csv file with each line formatted as YYYY-MM-DD,Name
Each entry has current age added to the name of the event.
How to use it:
Change the input filename specified in DATA_SOURCE.
YEARS_AHEAD constant specifies how many years of events are to be generated.
'''
import datetime
import csv
import ics
# setup
CURRENT_YEAR = int(datetime.datetime.now().strftime('%Y'))
YEARS_AHEAD = 10
DATA_SOURCE = 'bdays.csv' # YYYY-MM-DD,Name
birthdays = []
# Read the .csv data
with open(DATA_SOURCE, 'r') as fp:
data = csv.reader(fp, delimiter=',')
for entry in data:
birthdays.append((entry[0], entry[1]))
# create the calendar and the events
cal = ics.Calendar()
for birthday in birthdays:
bday = datetime.datetime.strptime(birthday[0], '%Y-%m-%d')
age = CURRENT_YEAR - bday.year
for i in range(0, YEARS_AHEAD):
e = ics.Event()
e.name = birthday[1] + ' ({})'.format(age + i)
e.begin = '{}-{:0>2}-{:0>2}'.format(CURRENT_YEAR + i,
bday.month,
bday.day)
e.make_all_day()
cal.events.add(e)
# dump the calendar data into .ics
ics_filename = DATA_SOURCE[:DATA_SOURCE.rfind('.')] + '.ics'
with open(ics_filename, 'w') as fp:
fp.write(str(cal))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment