[英]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.