Category Archives: Python

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 += "

daily links

· The guys at Search Engine Watch have a blog now…

· Python date handling tutorial

· Java Open Single Sign-On Project: an open source J2EE-based SSO infrastructure aimed to provide a solution for centralized platform neutral user authentication.

· Better Software magazine: delivers relevant, timely information to tackle the challenges of building better quality software, regardless of your role in the software development lifecycle.

· Better Software magazine on Continuous Integration

· CruiseControl: I need to start using this…

· No balls. No babies.

Extracting Text From MS Word

Someone on the Lucene User list wanted to know if it was possible to search MS Word documents using Lucene. The normal response is to go and take a look at the Jakarta POI project (new blog by the way). Ryan Ackley submitted his website (textmining.org) along with a plug for his TextMining.org Word Text Extractor v0.4 and some sample code:

FileInputStream in = new FileInputStream ("test.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText();

Nice.

Someone else noted that the Python version of Lucene (called Lupy) has an indexer for MS Word and PDF as well, although it appears to only work on Windows.