![](/img/trans.png)
[英]How download attachments from secondary outlook email by Python?
[英]Python download attachments from outlook, not looping thru each attachment per email
此代碼進入Outlook,並下載每封電子郵件的附件。 問題在於,每封電子郵件僅下載(1)。 我需要它來下載所有每封電子郵件。 這是我的第一個程序,任何技巧,建議都值得贊賞。 提前致謝!
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") #Opens Microsoft Outlook
folder = outlook.Folders[3] #N4 Invocie folder
subFolder = folder.Folders[5] #N4 Invoice subfolder
subFolderMessages = subFolder.Items #Invoice items object
message = subFolderMessages.GetFirst()
while True:
subFolderItemAttachments = message.Attachments
nbrOfAttachmentInMessage = subFolderItemAttachments.Count
x = 1
while x <= nbrOfAttachmentInMessage:
attachment = subFolderItemAttachments.item(x)
#Saves attachment to location
attachment.SaveAsFile('C:\\Users\\kkim\\Desktop\\InvoiceOutlook' + '\\'+ str(attachment))
break
message = subFolderMessages.GetNext()
移除while循環中附件的break
。
您的第一個while循環必須檢查message
變量!= null。
這應該工作
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") #Opens Microsoft Outlook
folder = outlook.Folders[3] #N4 Invocie folder
subFolder = folder.Folders[5] #N4 Invoice subfolder
subFolderMessages = subFolder.Items #Invoice items object
message = subFolderMessages.GetFirst()
for attachment in message.attachments:
attachment.SaveAsFile('C:\\Users\\kkim\\Desktop\\InvoiceOutlook' + '\\'+ str(attachment))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.