TL;DR: Install win32 extensions (e.g. using this to put it in your virtual env). Read some code below (there are folders with stuff, messages have CapitalizedPropertyNames). Profit.
The other day I needed to perform some statistics on an Outlook mailbox. In particular, use the frequency per day to identify peaks of activity. I could get an excel sheet going with VBA but it’s more fun in python. So, here it goes (mind you, it’s a bit long due to the included code).
Preparation
First, I’ve defined a class to help me with Outlook objects:
Now, like in C# or VB, the outlook object (MAPI) is a singleton. In python terms, I’m initialising and storing it as a global variable:
As you can notice, there’s also an exclusion list (I’ll use this later to skip folders).
Message Processor
The message processor is quite simple in the sense that it only spits out some message information:
The subject
The sent on date
The categories
Two notes:
I’m using categories as tags. It’s much easier grouping messages by subject with categories than moving them in subfolders, particularly since some messages have multiple categories and
No heavy lifting. Aggregation and statistics are left to other packages
The function takes the folder as a parameter and loops through messages:
Note: The messages in a folder can be retrieved via
. I could probably use the helper class but I haven’t tried it yet for the code.
Now, all I need is to get the folder.
Getting to the folder
To get to the actual Outlook folder object, I’ve implemented a simple path-based search engine:
The search function is pretty simple:
The
only performs a string search on the names:
Bonus: Pretty print
To make things a bit nicer and help with debugging, I’ve also created a pretty print function:
I used the exclusion list here to avoid going in those folders (too time consuming).
The full code
The full code (which can be quite verbose if DEBUG mode is enabled) is:
HTH,
A little experiment: If you find this post and ad below useful, please check the ad out