如何使用 Python 从 Outlook 下载电子邮件附件 [英] How to download email attachments from outlook using Python

查看:191
本文介绍了如何使用 Python 从 Outlook 下载电子邮件附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用我在这里找到的这个脚本.我想要做的是提供一些主题行或电子邮件,我将下载并保存附件.

这是我使用的代码:

导入日期时间导入操作系统导入 win32com.clientpath = os.path.expanduser("//cottonwood//users//MyDocuments//Projects//outlook crawler")今天 = datetime.date.today()Outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")收件箱 = Outlook.GetDefaultFolder(6)消息 = 收件箱.Items定义保存附件(主题):对于消息中的消息:如果 message.Subject == 主题和 message.Unread 或 message.Senton.date() == 今天:# body_content = message.body附件 = 消息.附件附件 = 附件.Item(1)用于 message.Attachments 中的附件:附件.SaveAsFile(os.path.join(path, str(attachment)))如果 message.Subject == 主题和 message.Unread:message.Unread = False休息保存附件(Snarf")

我收到此错误:

 File ">", line 2, in Itempywintypes.com_error: (-2147352567, '发生异常.', (4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)

Outlook 电子邮件是工作电子邮件,它是 Microsoft Outlook 2010.

我的问题是如何从 Microsoft Outlook 下载和保存附件.

解决方案

Items.Restrict Method (Outlook) 按主题行和附件进行过滤.请参阅过滤项目

导入 win32com.clientOutlook = win32com.client.Dispatch(Outlook.Application")olNs = Outlook.GetNamespace(MAPI")收件箱 = olNs.GetDefaultFolder(6)过滤器 = ("@SQL=" + chr(34) + "urn:schemas:httpmail:subject" +chr(34) + "像 'Snarf' AND "+chr(34) + "urn:schemas:httpmail:hasattachment";+chr(34) + "=1")项目 = Inbox.Items.Restrict(过滤器)对于项目中的项目:对于 Item.Attachments 中的附件:打印(附件.文件名)附件.SaveAsFile(rC:\\子文件夹\\"+附件.文件名)


<块引用>

使用字符串比较过滤项目 DASL 过滤器支持包括等价、前缀、短语和子字符串匹配.请注意,当您对 Subject 属性进行过滤时,诸如RE:"之类的前缀将不会出现.和FW:"被忽略.

I tried to use this script I found here. What I want to do is given either some subject line or email I will download and save the attachment.

This is the code I used:

import datetime
import os
import win32com.client


path = os.path.expanduser("//cottonwood//users///MyDocuments//Projects//outlook crawler")

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

saveattachemnts("Snarf")

I am getting this error:

  File "<COMObject <unknown>>", line 2, in Item
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Array index out of bounds.', None, 0, -2147352567), None)

The Outlook email is a work email and it is Microsoft Outlook 2010.

My question is how do I download and save attachments from Microsoft Outlook.

解决方案

Work with Items.Restrict Method (Outlook) to filter by subject line and attachment. see Filtering Items

import win32com.client

Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)

Filter = ("@SQL=" + chr(34) + "urn:schemas:httpmail:subject" +
                    chr(34) + " Like 'Snarf' AND " +
                    chr(34) + "urn:schemas:httpmail:hasattachment" +
                    chr(34) + "=1")

Items = Inbox.Items.Restrict(Filter)
for Item in Items:
    for attachment in Item.Attachments:
        print(attachment.FileName)
        attachment.SaveAsFile(r"C:\\subfolder\\" + attachment.FileName")


Filtering Items Using a String Comparison that DASL filters support includes equivalence, prefix, phrase, and substring matching. Note that when you filter on the Subject property, prefixes such as "RE: " and "FW: " are ignored.

这篇关于如何使用 Python 从 Outlook 下载电子邮件附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆