[英]How to save MS Outlook attachments from specific sender and date using Python
[英]How to save MS Outlook attachments from specific folder and hour using Python
我想要一個 Python 腳本來保存所有附件(帶有來自特定文件夾的 name_datesamp 並過濾特定時間戳(23:00)。
請你幫助我好嗎?
我正在使用以下代碼(在另一個線程中找到),但它只允許我使用標准名稱保存收件箱文件夾中包含的最新郵件的附件:
import win32com.client
import os.path
Outlook = win32com.client.Dispatch("Outlook.Application")
olNs = Outlook.GetNamespace("MAPI")
Inbox = olNs.GetDefaultFolder(6)
Filtermail = "[SenderEmailAddress] = 'xxx@yyy.com'"
Items = Inbox.Items.Restrict(Filtermail)
Item = Items.GetFirst()
for attachment in Items.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.getcwd() + '\\Mail\\' + 'zzzz.xlsx')
首先,您需要修改搜索條件以獲取特定時間范圍內的項目。
DateToCheck = "[RecievedTime] >= """ & DateStart & """"
其次,您需要遍歷循環中找到的所有項目,而不僅僅是獲取第一個並處理附件(VBA 語法):
Set myRestrictItems = myContacts.Restrict(DateToCheck)
For Each myItem In myRestrictItems
If (myItem.Class = olMail) Then
MsgBox myItem.Subject & ": " & myItem.RecievedTime
End If
Next
MailItem.ReceivedTime屬性返回一個Date
,指示接收項目的日期和時間。
第三,這里是帶有附件的項目的搜索查詢(VBA 語法):
query ="@SQL=" & chr(34) & "urn:schemas:httpmail:hasattachment" & chr(34) & "=True"
或者
query ="@SQL=" & chr(34) & "urn:schemas:httpmail:hasattachment" & chr(34) & "=1"
The samples are in VBA because I am not familiar with Python syntax, but the Outlook object model is common for all kind of programming languages. 所以,希望這不應該是一個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.