Skip to content

Instantly share code, notes, and snippets.

@waylan
Created June 15, 2022 20:06
Show Gist options
  • Save waylan/a447b84a5fc971feb8f59179a07d0b15 to your computer and use it in GitHub Desktop.
Save waylan/a447b84a5fc971feb8f59179a07d0b15 to your computer and use it in GitHub Desktop.
import icalendar
def rename_events(infile, outfile, names=None):
'''
Given a path to a ics file, rename each event and write to outfile.
names should be a list of strings where each item is the new name
to assign to each event in the ics file. If an insuffient number
of names are provided, then an error will occur.
If no names are provided, then the user is asked to provide a name
for each event from the command line. An empty response for an event
results in the existing name being preserved.
'''
# Read input file
with open(infile, 'rb') as f:
cal = icalendar.Calendar.from_ical(f.read())
if names:
# Use the provided names
for i, event in enumerate(cal.walk(name='VEVENT')):
event['summary'] = names[i]
else:
# Get names from user
for event in cal.walk(name='VEVENT'):
name = input(f'New Event Name ({event["summary"]}): ')
if name.strip():
event['summary'] = name
# write output file
with open(outfile, 'wb') as f:
f.write(cal.to_ical())
if __name__ == '__main__':
#rename_events('example.ics', 'modified.ics', names=['Modified Event'])
rename_events('example.ics', 'manual.ics')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment