【问题标题】:Unable to read 'Outlook item' object as attachment with win32com无法使用 win32com 将“Outlook 项目”对象作为附件读取
【发布时间】:2018-12-01 01:59:45
【问题描述】:

在使用 win32com 遍历 Outlook 中的特定文件夹时,我试图将遇到的任何附件的文件名作为字符串返回。当附件不是“Outlook 项目”(https://i.imgur.com/UauktNV.png)时,将文件名作为字符串返回时我没有任何问题——不幸的是,这是我实际上必须处理的唯一类型的附件。我面临的错误如下......

  File "C:/Users/bob/PycharmProjects/program/program.py", line 76, in handle_email
    attachment = attachments.Item(1)
  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)

我的代码如下

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
root_folder = outlook.Folders.Item(1)
test = root_folder.Folders['test']
messages = test.items

attachments = message.Attachments
attachment = attachments.Item(1)
name = attachment.FileName

如果我打印“附件”,我也会得到 &lt;COMObject &lt;unknown&gt;&gt;。这让我相信我可能没有从属性对象层次结构中接近对象,正如这里引用的 https://docs.microsoft.com/en-us/office/vba/api/outlook.attachment 虽然 AFAIK 它似乎是正确的,但可能是为什么当我尝试注释掉 attachmentthen 打印 name 我收到错误 AttributeError: &lt;unknown&gt;.FileName

注意:我的问题和这个类似 --> Filename is returned as null when an outlook item (.msg file) is added as attachment to an outlook email sent

但是,我什至无法打印空值,而且我尝试读取的附件有文件名。

谢谢。

【问题讨论】:

    标签: python outlook win32com


    【解决方案1】:

    COM error in downloading attachment from outlook through win32com

    ^ 这就是解决方案。我试图返回最初并不总是存在的附件。

    新代码如下

    attachment = message.Attachments
    count = message.Attachments.Count
    if count > 0:
        attachment = attachment.Item(1)
        print("[+] Begin attachment")
        print(attachment)
        print("[-] End attachment")
    

    【讨论】:

      猜你喜欢
      • 2021-12-01
      • 1970-01-01
      • 2022-01-15
      • 2018-10-27
      • 1970-01-01
      • 2019-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多