使用Python保存Outlook附件

时间:2016-02-17 00:22:55

标签: python parsing outlook

我正在尝试将附件(文件是NRG原始数据文件)从Outlook电子邮件保存到我的桌面,但是收到以下错误:“AttributeError:< unknown> .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)

谢谢

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我知道这是一个老帖子,但是:

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

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

也不应该在“未排序”之后有\\\,或者您是将文件保存到桌面并调用它UnsortedYourFileAttachmentNameHere.xls,因此工作线应该是:

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

答案 2 :(得分:0)

“。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)