Easily Automate Your Outlook Email with Python

Hridai Trivedy
4 min readMay 4, 2021

win32com allows you to automate boring Outlook e-mail operations — and it’s surprisingly easy to use!

Shave hours off your week using the win32com library

Most of us know what it’s like to log into our computer in the mornings and find ourselves awash in a sea of unread e-mails. We then go through the rigmarole of saving down attachments, moving e-mails across folders and generally sorting through it all in the same repetetive manner as we did the day before.

This sort of repetetive, rule-based task is ripe for automation and all you need automate your Outlook is to have Python and a library called win32com downloaded.

Getting Started

I will assume you already have Microsoft Outlook installed and are using it to manage your e-mails. You will just have to install python 3 . Once you have python installed, you will be able to install the win32com library using your command prompt by entering the below command

pip install pywin32

This will allow you to import this module, into a Python script, like so

import win32com.client

If you have your Outlook already open, you are now ready to interact programatically with it using Python and start automating away the really boring tasks!

Example Script

In this example we will look at the below process which has 2 steps:

  • Save all attachments, found in all e-mails which sit in a particular Outlook folder, to a C Drive directory
  • Move the e-mails to another Outlook folder once attachment is processed

The meat of the script revolves around the function runOlScriptwhich takes in a few arguments:
outdest — File path to the directory you wish to save to.
filefmt — Defines which file format to save (e.g. save all .csv files found in folder).
olreadfolder — The subfolder in your inbox to scan for attachments .
olprocessedfolder — The subfolder to the above folder which the processed e-mail will be moved to.
proc — The process being called. you can either save the e-mail attachments or the e-mail text bodys in this example.

The pertinent parts of the function are below. The full verbose code is available for download from my Github

In summary the code above works like this: the function finds your inbox and named subfolder, iterates through all of the mail objects in there and carries out any logic you have defined in the code. In our case it looks for any attachments, extracts the filename and appends it to your C drive file path and then saves it down. Finally it moves this mail object to another named Outlook folder (which I like to do as it denotes that the mail has been ‘processed’).

A visual example of this process is below. Outlook is on the left of the screen, the destination C Drive folder is on the right and the command prompt which triggers the script is at the very bottom.

The e-mail’s subject and filename are combined to form the final filname which is used to save the file in the output C Drive folder. A common alternative is to use the date the e-mail was sent on too — this example is also in the full Github code
It’s as easy as this to execute— and easy to slot into a larger batch job. The e-mail, once processed, is moved from folder “test” to subfolder “testout”.

Scheduling and Automating the Job

Assuming your using a windows operating system, automating this script is as simple as creating a .bat file and a job in the job scheduler which once you have chosen your input arguments is trivial to set up. This guide will help you set this up if you’ve never done so before. Once set up this script will fire at days/times/intervals of your choosing and will make short work of any regular task you carry out on the back of any e-mail.

The arguments you will need to provide are at the bottom of the script in the full script (found here on my Github) and summarised here

You must include one of --olatt and --olbody . That tells the program whether to save the attachments or just the text of the body of the e-mail down.
--outdest is the file path of the directory you wish to save to.
--olfolder is the name of the folder in your inbox which contains the e-mails you wish to process.
--olprocfolder is the nested folder to which the processed e-mail(s) get moved to. Note the way the code is set up: this olprocfolder must be nested under the olfolder. It is easy to tweak this default behaviour — just reach out to me if you get stuck!

Use Cases

You can do literally anything that is rule based and recurring in Outlook and it is easy to once you have had a look at Microsoft’s documentation on Outlook objects.

My favourite use cases are:

  • Save .xlsx / .csv files down, process their contents using pandas and save to DB as part of a larger ETL pipeline.
  • Programatically reply to e-mails in a specific way if they are from certain senders at certain time windows.
  • Check who has RSVP’d from the mailing list of an e-mail you previously sent and create a spreadsheet containing their details and any requirements they may have included in their e-mail replies.

Finally

Documentation on the Outlook objects are all here and you’ll need to consult this if you are trying to do anything a little more sophisticated. https://docs.microsoft.com/en-us/office/vba/api/overview/outlook/object-model

For anything else — you can find my github here. When I’m not surfing or building robots, I’m most probably at my computer tinkering with new ways to automate the really boring stuff so please don’t hesitate to reach out!
https://github.com/Hridai

--

--

Hridai Trivedy

Python Quant | Computer Vision and Robot Enthusiast | MMA Sports Modelling | Automation Expert | Surfer | Sydney, Australia