簡體   English   中英

Python從Outlook下載附件,而不循環通過每個電子郵件的每個附件

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM