Photo by Finding Dan | Dan Grinwis on Unsplash

Automating Outlook Calendar Downloads with Python

(because life’s too short to waste it on manual tasks)

  1. Filter the appointments collection to a specified date range
  2. Extract specific attributes
  3. Group the results by date
  4. Save to .csv file
  1. Install any of the software

Step 1— The win32com.client and python

First: Fire up a new Jupyter Notebook/Lab session. I prefer Jupyter Lab as you’ll see from my screenshots. Set up the library calls:

Libraries needed in this Notebook
# Step 1, block 1 : access Outlook and get events from the calendar
Outlook = win32com.client.Dispatch(“Outlook.Application”)
ns = Outlook.GetNamespace(“MAPI”)
appts = ns.GetDefaultFolder(9).Items
# Step 1, block 2 : sort events by occurrence and include recurring events
appts.IncludeRecurrences = “True”
  1. I then sort the appointments folder by Start date, and set the flag to include recurring events.

Step 2— Filter appointments to a date range

Add the following code to a new cell in your Jupyter Notebook:

# Step 2, block 1 : filter to the range: from = (today — 10), to = (today)
end =“%m/%d/%Y”)
begin = — datetime.timedelta(days=10)
begin = begin.strftime(“%m/%d/%Y”)
appts = appts.Restrict(“[Start] >= ‘“ +begin+ “‘ AND [END] <= ‘“ +end+ “‘“)

Step 3 — Extract specific attributes

Add the following code to a new cell in your Jupyter Notebook:

# Step 3, block 1 create list of excluded meeting subjects
excluded_subjects=(‘<first excluded subject>’, ‘<second excluded subject>’,
‘<third excluded subject>’, ‘<etc … >’)
# Step 3, block 2 : populate dictionary of meetings
apptDict = {}
item = 0
for indx, a in enumerate(appts):
subject = str(a.Subject)
if subject in (excluded_subjects):
organizer = str(a.Organizer)
meetingDate = str(a.Start)
date = parse(meetingDate).date()
subject = str(a.Subject)
duration = str(a.duration)
apptDict[item] = {“Duration”: duration, “Organizer”: organizer, “Subject”: subject, “Date”: date.strftime(“%m/%d/%Y”)}
item = item + 1
  1. Then I iterate over the collection of appointments and build a dictionary of appointments with just the attributes I need. These are the ‘Duration’ of the meeting (to bill my time), the ‘Organizer’ of the meeting, the ‘Subject’, and the ‘Date’. I arrange those fields in that order and add them to the dictionary using an index ‘key’

Step 4 — Group the results by date

Add the following code to a new cell in your Jupyter Notebook:

# Step 4, block 1 : convert discretionary to dataframe and group_by Date
apt_df = pd.DataFrame.from_dict(apptDict, orient=’index’, columns = [‘Duration’, ‘Organizer’, ‘Subject’, ‘Date’])
apt_df = apt_df.set_index(‘Date’)
apt_df[‘Meetings’] = apt_df[[‘Duration’, ‘Organizer’, ‘Subject’]].agg(‘ | ‘.join, axis=1)
grouped_apt_df = apt_df.groupby(‘Date’).agg({‘Meetings’:’, ‘.join})
grouped_apt_df.index = pd.to_datetime(grouped_apt_df.index)

Step 5 — Save to .csv file

Add the following code to a new cell in your Jupyter Notebook:

# Step 5, block 1: add timestamp to filename and save
filename =“%Y%m%d”) + ‘_10day_meeting_list.csv’
grouped_apt_df.to_csv (filename, index = True, header=True)