close

How to download outlook attachment from Python Script?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to download outlook attachment from Python Script in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to download outlook attachment from Python Script?

  1. How to download outlook attachment from Python Script?

    The below code helps by downloading the attachments from outlook emails that are
    'Unread' (and changes the mail to Read.) or from 'Today's' date.

  2. download outlook attachment from Python Script

    The below code helps by downloading the attachments from outlook emails that are
    'Unread' (and changes the mail to Read.) or from 'Today's' date.

Method 1

The below code helps by downloading the attachments from outlook emails that are

  • Unread‘ (and changes the mail to Read.) or from ‘Today’s‘ date.
  • without altering the file name.

Just pass the ‘Subject‘ argument.

import datetime
import os
import win32com.client


path = os.path.expanduser("~/Desktop/Attachments")
today = datetime.date.today()

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) 
messages = inbox.Items


def saveattachemnts(subject):
    for message in messages:
        if message.Subject == subject and message.Unread or message.Senton.date() == today:
            # body_content = message.body
            attachments = message.Attachments
            attachment = attachments.Item(1)
            for attachment in message.Attachments:
                attachment.SaveAsFile(os.path.join(path, str(attachment)))
                if message.Subject == subject and message.Unread:
                    message.Unread = False
                break

Method 2

import win32com.client #pip install pypiwin32 to work with windows operating sysytm
import datetime
import os

# To get today's date in 'day-month-year' format(01-12-2017).
dateToday=datetime.datetime.today()
FormatedDate=('{:02d}'.format(dateToday.day)+'-'+'{:02d}'.format(dateToday.month)+'-'+'{:04d}'.format(dateToday.year))

# Creating an object for the outlook application.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
# Creating an object to access Inbox of the outlook.
inbox=outlook.GetDefaultFolder(6)
# Creating an object to access items inside the inbox of outlook.
messages=inbox.Items

def save_attachments(subject,which_item,file_name):
    # To iterate through inbox emails using inbox.Items object.
    for message in messages:
        if (message.Subject == subject):
            body_content = message.body
            # Creating an object for the message.Attachments.
            attachment = message.Attachments
            # To check which item is selected among the attacments.
            print (message.Attachments.Item(which_item))
            # To iterate through email items using message.Attachments object.
            for attachment in message.Attachments:
                # To save the perticular attachment at the desired location in your hard disk.
                attachment.SaveAsFile(os.path.join("D:\Script\Monitoring",file_name))
                break

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read