![](/img/trans.png)
[英]How to save MS Outlook attachments from specific folder and hour using Python
[英]How to save MS Outlook attachments from specific sender and date using Python
我對編碼有點陌生,我試圖了解如何讓 Python 保存來自特定發件人的 MS Outlook 附件。 我目前每天都會收到來自同一個人的關於我需要保存到特定文件夾的數據的相同電子郵件。 以下是我試圖滿足的要求:
我看過一些關於使用 win32com.client 的帖子,但沒有太多運氣讓它與 MS Outlook 一起工作。 我將附上我在下面嘗試過的一些代碼。 我感謝任何反饋!
import win32com.client
outlook=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
你差不多明白了,為發件人電子郵件地址添加過濾器
import win32com.client
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)
Filter = "[SenderEmailAddress] = '0m3r@email.com'"
Items = Inbox.Items.Restrict(Filter)
Item = Items.GetFirst()
for attachment in Item.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(r"C:\path\to\my\folder\Attachment.xlsx")
Windows 上的 Python 3.8
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特別是它的屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.