**This will only work on Windows PC Devices and Microsoft Outlook email client**
Learn how to automate Microsoft Outlook tasks with Python
Congratulations! If you made it to this webpage, you’ve decided to start automating your Outlook email tasks with Python!
This website is dedicated to users like you – people who agree life is too short to be spent copying and pasting the same reports, emails, or status updates. Sure, at first learning how to automate sending Outlook emails with Python only seems like it would save a couple minutes a day. But once you add up – say 10 minutes of mundane tasks a day – that time over a year – it makes out to over a week spent just copying and pasting!
Plus, learning Python is a highly valuable skill for your career. It’s one of the rare skills that is better learned at your own pace too. Many self-taught coders quit early since they can’t automatically apply python skills to their job. Coding For the Rest of Us is dedicated providing complete automation code tutorials to help you learn Python and Outlook automation tasks fast. Once you see how the code works, it is so much easier to learn the fundamentals of python!
Getting Started
You will need:
- A Computer that is running Microsoft Windows (this will not work on an Apple Computer / Mac)
- Python installed on your computer
- Microsoft Outlook installed on your computer
First, import the win32com.client and os packages.
These packages allow you to perform outlook automation tasks.
Python Code
import win32com.client
import os
#having issues installing win32com? try the below (without the hashtag)
#pip install pypiwin32
#pip install pywin32
#python -m pip install pypiwin32
Reading an Outlook Email with Python
Once the above packages are imported, we are now ready to read our first Outlook email using Python! The below script will read your last Outlook email using Python and print out the following.
Reading Your Last Outlook Email
#Enter Folder Name Below (case insenstive)
folder_name = 'Inbox'
outlook_application = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
#If you have multiple mailboxes in outlook, make sure its correct. Try increasing the Item number in the line below
#Example : outlook_mailbox = outlook_application.Folders.Item(2)
outlook_mailbox =outlook_application.Folders.Item(1)
print("Reading The Last Email in "+ folder_name +" for: " + str(outlook_mailbox) + '
')
#finds last email in folder
folder = outlook_mailbox.Folders(folder_name)
folder_emails = folder.Items #gets all emails in inbox
folder_emails.Sort("[ReceivedTime]", True) #sorts by last received
#gets the last email (excludes calendar invites)
last_email = next((e for e in folder_emails if e.MessageClass =='IPM.Note') ,None)
#All Email Information
email_sender = last_email.Sender #who sent the email
email_recipients = last_email.To #who the email was sent to
email_date = last_email.ReceivedTime #which day the email was sent
email_subject = last_email.Subject #email subject
email_attachments = len(last_email.attachments) # email attachment count (inline photos count as attachments)
email_body = last_email.Body #email body
#prints out email information
print("Email Sender: " + str(email_sender))
print("Email Recipients: " + str(email_recipients))
print("Email Date Received: " + str(email_date))
print("Email Subject: " + str(email_subject))
print("Email Attachments: " + str(email_attachments))
print("Email Body: " + str(email_body))
Output:
Reading The Last Email in 'Inbox' for: 'YOUR_EMAIL_ADDRESS' Email Sender: SENDER_NAME Email Recipients: RECIPIENT_NAME Email Date Received: 2022-09-03 10:06:08.482000+00:00 Email Subject: EMAIL_SUBJECT Email Attachments: ATTACHMENT_COUNT Email Body: EMAIL_BODY
Saving an Outlook Email Attachment to A Folder with Python
In order to automate downloading Outlook attachments with Python, we need to provide the program information about what emails we are looking for. This includes
- folder_name = which folder the email lives in (Inbox? or a Custom Outlook Folder?)
- subject_line = a phrase that will help us identify the correct email via subject line (i.e find the last email that contains “Weekly Report”)
- save_location = this is the file path where you want the email attachment to automatically save to (The Example Code is set to the Desktop)
Saving an Outlook Email Attachment to A Folder with Python
#Enter Folder Name Below (case insenstive)
folder_name = 'ENTER_FOLDER_NAME_HERE'
subject_line = 'ENTER_SUBJECT_LINE_HERE' #finds anything that contains this in the subject line - case insensitive
#keep the r in front of the file path , it will not work without it
save_location = r'C:\Users\YOUR_USER_NAME_HERE\Desktop'
outlook_application = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
#If you have multiple mailboxes in outlook, make sure its correct. Try increasing the Item number in the line below
#Example : outlook_mailbox = outlook_application.Folders.Item(2)
outlook_mailbox =outlook_application.Folders.Item(1)
print("Saving Attachments from Emails with Subject "+ subject_line +" for: " + str(outlook_mailbox) + '\n')
#finds folder in outlook
folder = outlook_mailbox.Folders(folder_name)
folder_emails = folder.Items #gets all emails in inbox
folder_emails.Sort("[ReceivedTime]", True) #sorts by last received
#Loops Through Outlook Emails in Folder
for email in folder_emails:
#If email is not a calendar invite, and contains 'subject_line' in Subject and has at least 1 attachment
if (email.MessageClass =='IPM.Note') & ((subject_line.lower()) in (str(email.Subject.lower()))) & (len(email.attachments)>0):
#gets info about email and prints
email_date = email.ReceivedTime #which day the email was sent
email_subject = email.Subject #email subject
email_attachments = len(email.attachments) # email attachment count (inline photos count as attachments)
print('Saving Attachments from the below email:')
print("Email Date Received: " + str(email_date))
print("Email Subject: " + str(email_subject))
print("Email Attachments: " + str(email_attachments) + '\n')
#gets info about number of attachments and prints
num_email_attachments = len([x for x in email.attachments])+1
for attachment_num in range(1, num_email_attachments):
attachment = email.attachments.Item(attachment_num)
attachment_name = str(attachment)
print('Attachment Number ' + str(attachment_num) +' - File Name : '+attachment_name)
try:
save_file_path = os.path.join (save_location,attachment_name)
attachment.SaveAsFile(save_file_path)
print('Attachment ' + str(attachment) +" saved to " + save_file_path +'\n')
except:
print('Error could not save! Cannot save Outlook Emails as Attachment')
Output Saving Attachments from Emails with Subject 'ENTER_SUBJECT_LINE_HERE' for: 'YOUR_EMAIL_ADDRESS' Saving Attachments from the below email: Email Date Received: 2023-02-15 16:16:00.041000+00:00 Email Subject: Re: Report - September 2022 Email Attachments: 1 Attachment Number 1 - File Name : image001.png Attachment image001.png saved to C:\Users\YOUR_USER_NAME_HERE\Desktop\image001.png
Why Use Python Outlook Automation?
Using Python Outlook Automation can save you time and increase your productivity by automating repetitive tasks. For example, if you regularly send out the same type of email, you can use Python to automate the process, freeing up your time for other tasks. Additionally, by automating tasks, you can reduce the risk of errors that can occur when performing tasks manually.
Leave a Reply