首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用python从outlook下载所有附件?

如何使用python从outlook下载所有附件?
EN

Stack Overflow用户
提问于 2018-05-23 02:00:39
回答 2查看 6K关注 0票数 1

问题:

在一封邮件中,我将有多个附件,我需要下载所有附件。

我该怎么做呢?

我的代码:

代码语言:javascript
复制
import win32com.client
import os
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
messages = inbox.Items
message = messages.GetFirst()
subject = message.Subject
#
get_path = 'C:\\Users\\test\\Desktop\\resumes'

for m in messages:
    if m.Subject == "FW: Opportunity with Mindtree | Automotive Infotainment |":

        print (message)
        attachments = message.Attachments
        attachment = attachments.Item(1)
        attachment.SaveASFile(os.path.join(get_path,attachment.FileName)) #Saves to the attachment to current folder
        print (attachment)
        message = messages.GetNext()

    else:
        message = messages.GetNext()
EN

回答 2

Stack Overflow用户

发布于 2018-08-22 05:09:28

您的代码还存在其他问题,但这应该允许您获得多个附件:

代码语言:javascript
复制
import win32com.client
import os
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
messages = inbox.Items
message = messages.GetFirst()
subject = message.Subject
#
get_path = 'C:\\Users\\test\\Desktop\\resumes'

for m in messages:
    if m.Subject == "FW: Opportunity with Mindtree | Automotive Infotainment |":

        print (message)
        attachments = message.Attachments
        num_attach = len([x for x in attachments]))
            for x in range(1, num_attach):
            attachment = attachments.Item(x)
            attachment.SaveASFile(os.path.join(get_path,attachment.FileName))
        print (attachment)
        message = messages.GetNext()

    else:
        message = messages.GetNext()
票数 4
EN

Stack Overflow用户

发布于 2019-11-05 01:06:29

代码语言:javascript
复制
import win32com.client
import os
import datetime as dt

mydesktop = os.path.expanduser('~') + '/Desktop/'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

print (dt.datetime.now())

# setup range for outlook to search emails (so we don't go through the entire inbox)
lastWeekDateTime = dt.datetime.now() - dt.timedelta(days = 4)
lastWeekDateTime = lastWeekDateTime.strftime('%m/%d/%Y %H:%M %p')

# Select main Inbox
inbox = outlook.GetDefaultFolder(6)

# Optional:  Select main Inbox, look in subfolder "Test"
#inbox = outlook.GetDefaultFolder(6).Folders["Test"]

messages = inbox.Items

# Only search emails in the time range above:
messages = messages.Restrict("[ReceivedTime] >= '" + lastWeekDateTime +"'")

print ('Reading Inbox, including Inbox Subfolders...')

# Download a select attachment ---------------------------------------
# Create a folder to capture attachments.
Myfolder = mydesktop + 'Outlook Export/'
if not os.path.exists(Myfolder): os.makedirs(Myfolder)

try:
    for message in list(messages):
        try:
            s = message.sender
            s = str(s)
            print('Sender:' , message.sender)
            for att in message.Attachments:
                # Give each attachment a path and filename
                outfile_name1 = Myfolder + att.FileName
                # save file 
                att.SaveASFile(outfile_name1)
                print('Saved file:', outfile_name1)

        except Exception as e:
            print("type error: " + str(e))
            x=1

except Exception as e:
    print("type error: " + str(e))
    x=1

#Delete unused file types (like .png)-----------------------------------------

test = os.listdir(Myfolder)

for item in test:
    if item.endswith(".png"):
        os.remove(os.path.join(Myfolder, item))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50473865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档