Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
548 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Reading Outlook emails and downloading attachments using Python |
---|
This blog shows how you can use Python to loop over all of the emails in a folder in Outlook, saving the attachments for each to your hard disk. |
Is there any limit to what you can do easily in Python? This blog explains how you can loop over the emails in an Outlook folder, downloading all the attachments (although it contains lots of other hints and tips on working with Outlook from within Python).
This blog assumes that you have a reasonable knowledge of Python. It was written using Visual Studio Code in Windows 10. The best source reference page I found for working with Outlook in Python is this StackOverflow question.
To get this to work, you'll need to install the win32com.client module:
How to do this in Visual Studio Code.
If you still get missing module messages, try some of the ideas in my StackOverflow answer.
To make my code easier to read, I've used a Python enumeration to get a list of the main mail folders:
from enum import Enum
class OutlookFolder(Enum):
olFolderDeletedItems = 3 # The Deleted Items folder
olFolderOutbox = 4 # The Outbox folder
olFolderSentMail = 5 # The Sent Mail folder
olFolderInbox = 6 # The Inbox folder
olFolderDrafts = 16 # The Drafts folder
olFolderJunk = 23 # The Junk E-Mail folder
This isn't a complete list, but does contain the mail folders you're most likely to be working with.
The next thing to do is to get a reference to the mail folder that you want to use:
# the module to work with
import win32com.client as win32
# get a reference to Outlook
outlook = win32.Dispatch("Outlook.Application").GetNamespace("MAPI")
# get the Inbox folder (you can a list of all of the possible settings at https://docs.microsoft.com/en-us/office/vba/api/outlook.oldefaultfolders)
inbox = outlook.GetDefaultFolder(OutlookFolder.olFolderInbox.value)
# get subfolder of this
todo = inbox.Folders.Item("To do - home")
Here I'm going to look at the emails in this folder, which is a subfolder of my inbox:
I have a lot of to do lists!
The rest of my code reads the details for each message in the folder into a tuple, adds this tuple to a list and then loops over the list processing each message in turn:
# get all the messages in this folder
messages = todo.Items
# check messages exist
if len(messages) == 0:
print("There aren't any messages in this folder")
exit()
# loop over them all
emails = []
for message in messages:
# get some information about each message in a tuple
this_message = (
message.Subject,
message.SenderEmailAddress,
message.To,
message.Unread,
message.Senton.date(),
message.body,
message.Attachments
)
# add this tuple of info to a list holding the messages
emails.append(this_message)
# show the results
for email in emails:
# unpack the tuple to get at information
subject, from_address, to_address, if_read, date_sent, body, attachments = email
# show the subject
print(subject, to_address)
# number of attachments
if len(attachments) == 0:
print("No attachments")
else:
for attachment in attachments:
attachment.SaveAsFile("c:\\__ajb\\" + attachment.FileName)
print("Saved {0} attachments".format(len(attachments)))
Remarkably, this simple routine saves all of the attachments for messages in the given folder to a folder on my hard disk!
Some other pages relevant to the above blog include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.