我对编码有点陌生,我试图了解如何获取Python以保存特定发件人的MS Outlook附件。目前,我每天都会收到同一个人发来的关于我需要保存到特定文件夹的数据的同一封电子邮件。以下是我要满足的要求:
我已经看到了一些有关使用win32com.client的文章,但运气不佳,无法与MS Outlook一起使用。我将在下面附加一些我尝试过的代码。感谢您的反馈!
导入win32com.clientoutlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages=inbox.Items
for message in messages:
attachments = message.attachments
for attachment in attachments:
pass
def saveAttachments(email:object):
for attachedFile in email.Attachments: #iterate over the attachments
try:
filename = attachedFile.FileName
attachedFile.SaveAsFile("C:\\EmailAttachmentDump\\"+filename) #Filepath must exist already
except Exception as e:
print(e)
for mailItem in inbox.Items:
#Here you just need to bould your own conditions
if mailItem.Sender == "x" or mailItem.SenderName == "y":
saveAttachments(mailItem)
您可以根据自己的喜好更改实际条件。我建议参考Outlook MailItem对象的对象模型:https://docs.microsoft.com/en-gb/office/vba/api/outlook.mailitem特别是其Properties