Use Python to Save Outlook Attachments to A Folder

**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.

  1. folder_name = which folder the email lives in (Inbox? or a Custom Outlook Folder?)
  2. 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”)
  3. 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!


Comments

13 responses to “Use Python to Save Outlook Attachments to A Folder”

  1. It’s perfect time to make a few plans for the long run and it’s time to be happy. I’ve learn this publish and if I may I wish to recommend you few fascinating things or advice. Maybe you could write subsequent articles regarding this article. I desire to read more issues about it!

  2. Hello there! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in trading links or maybe guest authoring a blog article or vice-versa? My website covers a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Terrific blog by the way!

  3. It is always great to come across a page where the admin take an actual effort to generate a really good article. Check out my website Webemail24 concerning about Website Design.

  4. This was a delight to read. You show an impressive grasp on this subject! I specialize about Weird & Funny News and you can see my posts here at my blog Seoranko Keep up the incredible work!

  5. Superb and well-thought-out content! If you need some information about Accountancy, then have a look here ArticleHome

  6. It is always great to come across a page where the admin take an actual effort to generate a really good article. Check out my website Autoprofi concerning about Car Purchase.

  7. Hey, if you are looking for more resources, check out my website Articlecity as I cover topics about SEO. By the way, you have impressive design and layout, plus interesting content, you deserve a high five!

  8. Great site with quality based content. You’ve done a remarkable job in discussing. Check out my website Article Star about Mobile Gaming and I look forward to seeing more of your great posts.

  9. I like how well-written and informative your content is. You have actually given us, your readers, brilliant information and not just filled up your blog with flowery texts like many blogs today do. If you visit my website FQ5 about Cosmetics, I’m sure you can also find something for yourself.

  10. I came across your site wanting to learn more and you did not disappoint. Keep up the terrific work, and just so you know, I have bookmarked your page to stay in the loop of your future posts. Here is mine at 46N about Cosmetics. Have a wonderful day!

  11. hyKSRMwu CGmEnmwb LCEaWd fPB wjRpg

Leave a Reply

Your email address will not be published. Required fields are marked *