**This will only work on Windows PC Devices and Microsoft Outlook email client**
Use pywin32 to Save Outlook Email Attachments to any Folder on your Computer
Do you regularly receive emails that contain attachments that need to be saved to a local folder each week? You can save tons of time automating this outlook task by using Python to save email attachments to any folder.
You will need python installed on your Windows PC computer (this will NOT work on a Mac). We recommend running this in a Jupyter Notebook, so you can easily run each code block, section by section.
First, import the win32com.client and os packages.
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
How to Use Python to Save Outlook Attachments to A Folder with pywin32
In order to start saving outlook attachments to a folder on your computer automatically with Python, you need to define the below criteria.
- 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 below uses Desktop.
#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')
Expected 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
Congratulations! You now know how to automate another Outlook task using Python and the pywin32com package. Learning how to automatically save attachments from emails to a local folder with Python can save you tons of time over the course of your career. Need this code as a reusable method instead? Try our article How to Automatically Download Outlook Email Attachments with Python next!
Leave a Reply