繁体   English   中英

使用Python保存Outlook附件

[英]Saving Outlook attachments with Python

我正在尝试将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)

谢谢

尝试这个:

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

这应该将文件复制到当前工作目录。 然后,您可以使用shutil模块将文件复制到所需的目标位置:

    shutil.copy(src, dst)

或者您可以使用以下方法移动文件而不是复制文件:

    shutil.move(src, dst)

这是关闭的文档: https ://docs.python.org/2/library/shutil.html

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

对于每个目录级别,文件位置不应使用两个\\,因此:

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

另外,在“未排序”之后,您不应该使用\\\\\\ ,还是将文件保存到桌面并UnsortedYourFileAttachmentNameHere.xls因此工作行应为:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM