【问题标题】:Saving Outlook attachments with Python使用 Python 保存 Outlook 附件
【发布时间】:2016-05-28 12:05:51
【问题描述】:

我正在尝试将 Outlook 电子邮件中的附件(文件是 NRG 原始数据文件)保存到我的桌面,但收到以下错误:“AttributeError: .SaveAsFile”

一切正常(我认为),直到我尝试保存文件...

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")



inbox = outlook.Folders["myinboxfolder"].Folders["Inbox"].Folders["[folder i need]"]
messages = inbox.Items
message = messages.GetLast()
attachment = message.attachments

attachment.SaveAsFile('C:\Users\my name \Desktop\Unsorted' + attachment.FileName)

谢谢

【问题讨论】:

    标签: python parsing outlook


    【解决方案1】:

    试试这个:

        attachment.SaveASFile(os.getcwd() + '\\' + attachment.FileName)
    

    这应该将文件复制到您当前的工作目录。然后您可以使用 shutil 模块将文件复制到您想要的目的地:

        shutil.copy(src, dst)
    

    或者你可以移动文件而不是复制使用:

        shutil.move(src, dst)
    

    这里是 shutil 文档:https://docs.python.org/2/library/shutil.html

    【讨论】:

      【解决方案2】:

      我知道这是一篇旧帖子,但是:

      文件位置不应该为每个目录级别使用两个\,所以:

      attachment.SaveAsFile('C:\\Users\\my name \\Desktop\\Unsorted' + attachment.FileName)
      

      你不应该在“未排序”之后有\\\,或者你是否将文件保存到桌面并调用它UnsortedYourFileAttachmentNameHere.xls 所以工作行应该是:

      attachment.SaveAsFile('C:\\Users\\YOURUSERNAMEHERE\\Desktop\\Unsorted\\' + attachment.FileName)
      

      【讨论】:

        【解决方案3】:

        “.SaveAsFile”应该用在'attachment.Item'上

        inbox = outlook.Folders["myinboxfolder"].Folders["Inbox"].Folders["[folder i need]"]
        messages = inbox.Items
        message = messages.GetLast()
        attachment = message.attachments
        attachment_item = attachment.Item
        
        attachment_item.SaveAsFile('C:\Users\my name \Desktop\Unsorted' + attachment_item.FileName)
        

        或者,您可以循环浏览附件

        inbox = outlook.Folders["myinboxfolder"].Folders["Inbox"].Folders["[folder i need]"]
        messages = inbox.Items
        message = messages.GetLast()
        attachment = message.attachments
        
        for i in attachment:
            i.SaveAsFile('C:\Users\my name \Desktop\Unsorted' + i.FileName)
        

        【讨论】:

          猜你喜欢
          • 2022-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-14
          • 2021-09-22
          • 2023-02-02
          • 1970-01-01
          • 2014-04-19
          相关资源
          最近更新 更多