[英]Saving Outlook attachments with Python
I'm trying to save attachments (the files are NRG raw data files) from outlook emails to my desktop, but am receiving the following error: "AttributeError: <unknown>.SaveAsFile" 我正在尝试将Outlook电子邮件中的附件(文件是NRG原始数据文件)保存到我的桌面上,但是收到以下错误:“ AttributeError:<未知> .SaveAsFile”
Everything works fine (I think) until I try to save the file... 在我尝试保存文件之前,一切正常(我认为)。
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)
Thank you 谢谢
Try this: 尝试这个:
attachment.SaveASFile(os.getcwd() + '\\' + attachment.FileName)
This should copy the file to your current working directory. 这应该将文件复制到当前工作目录。 Then you could use the shutil module to copy the file to the destination you desire:
然后,您可以使用shutil模块将文件复制到所需的目标位置:
shutil.copy(src, dst)
or you can move the file instead of copying using: 或者您可以使用以下方法移动文件而不是复制文件:
shutil.move(src, dst)
Here are the shutil docs: https://docs.python.org/2/library/shutil.html 这是关闭的文档: https ://docs.python.org/2/library/shutil.html
I know this is an old post but: 我知道这是一个旧帖子,但是:
shouldn't the file location be using two \\'s for each directory level, so: 对于每个目录级别,文件位置不应使用两个\\,因此:
attachment.SaveAsFile('C:\\Users\\my name \\Desktop\\Unsorted' + attachment.FileName)
Also shouldn't you have a \\\\
or \\
after "Unsorted", or are you saving the file to your desktop and calling it UnsortedYourFileAttachmentNameHere.xls
so the working line should be: 另外,在“未排序”之后,您不应该使用
\\\\
或\\
,还是将文件保存到桌面并UnsortedYourFileAttachmentNameHere.xls
因此工作行应为:
attachment.SaveAsFile('C:\\Users\\YOURUSERNAMEHERE\\Desktop\\Unsorted\\' + attachment.FileName)
The ".SaveAsFile" should be used on 'attachment.Item' “ .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)
or, you can loop through Attachments 或者,您可以循环浏览附件
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.