繁体   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