Skip to content

Instantly share code, notes, and snippets.

@SonOfLilit
Created November 5, 2014 01:55
Show Gist options
  • Save SonOfLilit/4269af8fcf1cd57171bb to your computer and use it in GitHub Desktop.
Save SonOfLilit/4269af8fcf1cd57171bb to your computer and use it in GitHub Desktop.
Make an .ics out of events in a weird Excel file (pre-converted to .csv)
import csv
from icalendar import Calendar, Event
from datetime import datetime, timedelta
from icalendar import LocalTimezone
HOUR = 60*60
start_hour = timedelta(0, 17*HOUR)
day_duration = timedelta(0, 4*HOUR)
r = csv.reader(open('e:\\temp\\luz.csv', 'rb'))
cal = Calendar()
cal.add('version', '2.0')
cal.add('prodid', '-//eisp social program schedule//thesocialprogram.co.il//')
cal.add('X-WR-CALNAME','EISP Social Program Schedule' )
lt = LocalTimezone() # we append the local timezone to each time so that icalendar will convert
# to UTC in the output
now = datetime.now().replace(tzinfo=lt)
i = iter(r)
i.next() # skip header row
activities = []
# topic and date appear only some of the times
topic, date = None, None
for mtopic, title, mdate in r:
topic = mtopic.decode('windows-1255') or topic
date = mdate.decode('windows-1255') or date
title = title.decode('windows-1255')
day, month = map(int, date.split('.'))
d = datetime(year=2014 if month >= 11 else 2015, month=month, day=day).replace(tzinfo=lt)
activity = [topic, title, d]
if mdate: # new date
activities.append([activity])
else:
activities[-1].append(activity)
for day_activities in activities:
hour = start_hour
delta = day_duration / len(day_activities)
for topic, title, day in day_activities:
start = (day + hour)
hour += delta
end = (day + hour)
event = Event()
event.add('summary', u'%s: %s' % (topic, title))
event.add('dtstart',start)
event.add('dtend', end)
event.add('dtstamp', now)
#event['uid'] = ent.pk # should probably use a better guid than just the PK
#event.add('priority', 5)
cal.add_component(event)
open('social.ics', 'wb').write(cal.to_ical())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment