Sending your Outlook calendar using Python

At work we’re required to send an email with our personal schedules at the beginning of the month to everyone in our group so that (at least until your schedule changes) everyone knows where everyone else is. Sure, we could be using a online calendar or some kind of group collaboration software but we’re stuck using Lotus Notes, which is the worst email client I’ve ever used (don’t believe me?). I switched to MS Outlook because a) I’ve used it before and b) the Notes Connector gives me access to the Notes server. Anyway, I’ve been wanting to tinker with Python for the last couple months and this seemed like the perfect project for it. Using the excellent win32com package, I was able to get a collection of all the items in my Outlook calendar, filter those events for the current month, put a message together with the subject, start date/end date and start time/end time and then send an email to an Outlook distribution list.

Since I’m new to Python, I’m going to walk through the code, narrating what I *think* is happening as I go along. A link to the finished product is available at the end, please let me know what, if anything, I’m doing wrong…

First things first, I import the appropriate modules (or packages?):

import win32com.client
import time
import datetime

After that I dive headfirst into Outlook getting an Outlook Application object, log on to the MAPI namespace, and retrieve all the items in the Outlook Calendar. Most examples I saw using ‘olCalendar’ as the argument to the GetDefaultFolder() method, but got the impression that ‘olCalendar’ is some sort of intrinsic constant that I didn’t have access too. This page that listed all the Outlook constants was where I got the number nine.

outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
appointments = namespace.GetDefaultFolder(9).Items

Next I sort the events by Start Date (right?) and flip the ‘include recurring events’ flag to true:

appointments.Sort("[Start]")
appointments.IncludeRecurrences = "True"

After that I do a bit of date munging to get the starting month/year and the last day of the month/year, use the Restrict() method to filter out only events in the current month and sort the events again:

# start date
monthNow = now.month
yearNow = now.year
begin = datetime.date(now.year, now.month, 1)
# end date
nextMonth = begin + datetime.timedelta(days=35)
end = datetime.date(nextMonth.year, nextMonth.month, 1)
appointments = appointments.Restrict(
  "[Start] >= '" + begin.strftime("%m/%d/%Y") +
  "' AND [End]
Now I'm left with a filtered and sorted collection of Outlook calendar items so the next step is to loop over the results, adding the subject, start time and end time to a message string:

appointment = appointments.GetFirst()
message = begin.strftime("%B, %Y") + " Meetings & Events for Aaron Johnson\n"
message += "

3 thoughts on “Sending your Outlook calendar using Python”

  1. Aaron, do you still have this code sitting around gathering dust somewhere? It looks like you have a good start but there are a few holes that need to be filled and my knowledge of Outlook is limited enough to hinder my further development. Any assitance would be greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *