-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvToIcs.py
59 lines (49 loc) · 1.98 KB
/
csvToIcs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## @file csvToIcs.py
#
# @brief this file converts CSV file to ICS
# Copyright 2022, Jodie Moh, All rights reserved.
#
# @author Jodie Moh
from os.path import expanduser, isdir
import datetime
import pandas as pd
from icalendar import Calendar, Event, LocalTimezone
import easygui
from random import randint
#CSV must have the following headers in the exact format
# Subject
# Start Date - MM/DD/YYYY
# Start Time - 12 AM/PM (01:45 PM)
# End Date - MM/DD/YYYY
# End Time - 12 AM/PM (01:45 PM)
# Location
# Description
def parseIcsFromCsv():
start_dir = '~/'
if isdir(expanduser("~/Desktop")):
start_dir = '~/Desktop/'
msg = 'Please select the .csv file to be converted to .ics'
fileName = easygui.fileopenbox(msg=msg, title="", default=expanduser(start_dir), filetypes=["*.csv"])
# Start calendar file
cal = Calendar()
cal.add('prodid', '-//ical conversion by pinkchocoa//')
cal.add('version', '2.0')
df = pd.read_csv(fileName)
for index, row in df.iterrows():
event = Event()
event.add('summary', row["Subject"])
timeStamp = row["Start Date"] + " " + row["Start Time"]
timeStamp = datetime.datetime.strptime(timeStamp, "%m/%d/%Y %I:%M%p")
event.add('dtstart', timeStamp)
timeStamp = row["End Date"] + " " + row["End Time"]
timeStamp = datetime.datetime.strptime(timeStamp, "%m/%d/%Y %I:%M%p")
event.add('dtend', timeStamp)
event.add('description', row['Description'])
event.add('location', row['Location'])
event.add('dtstamp', datetime.datetime.replace( datetime.datetime.now(), tzinfo=LocalTimezone() ))
event['uid'] = str(randint(1,10**30)) + datetime.datetime.now().strftime('%Y%m%dT%H%M%S')
cal.add_component(event)
# Write final .ics file to same directory as input file.
f = open(easygui.filesavebox(msg='Save .ics File', title='', default=expanduser('~/') + 'calendar.ics', filetypes=['*.ics']), 'wb')
f.write(cal.to_ical())
f.close()