簡體   English   中英

按Python在特定日期列出Outlook電子郵件

[英]listing Outlook emails by specific date in Python

我正在使用Python 3.我正在嘗試按日期提取(列表/打印節目)Outlook電子郵件。

我正在嘗試循環..可能是WHILE或IF語句。

可以這樣做,因為一個字符串,另一個是日期。 請介紹我到目前為止所得到的內容:謝謝。

 1. import win32com.client, datetime
 2. 
 3. # Connect with MS Outlook - must be open.
 4. outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
 5. # connect to Sent Items
 6. sent = outlook.GetDefaultFolder(5).Items  # "5" refers to the sent item of a folder
 7. 
 8. # Get yesterdays date
 9. y = (datetime.date.today () - datetime.timedelta (days=1))
 10. # Get emails by selected date        
 11. if sent == y: 
 12.     msg = sent.GetLast()
 13.     # get Subject line
 14.     sjl = msg.subject
 14.     # print it out                      
 15.     print (sjl)

outlook API有一個方法Items.Find ,用於搜索.Items的內容。 如果這是您想要做的程度,那可能就是您應該如何做到的。


現在看起來你的if語句正在檢查一組電子郵件是否等於昨天

Microsoft的文檔說.Items正在返回一組電子郵件,您首先必須使用幾種不同的方法(包括Items.GetNext或通過使用Items.GetNext引用特定索引來Items.Item

然后,您可以獲取當前的電子郵件並訪問.SentOn屬性。

currentMessage = sent.GetFirst()
while currentMessage:
    if currentMessage.SentOn == y:
        sjl = currentMessage.Subject
        print(sjl)
    currentMessage = sent.GetNext()

這應該遍歷發送文件夾中的所有消息,直到sent.GetNext()沒有更多消息要返回。 您必須確保y.SentOn返回的格式相同。

如果您不想遍歷每條消息,您可能還會嵌套兩個循環,這些循環返回到消息中,直到它到達昨天,迭代直到它不再在“昨天”之內,然后中斷。

我完成了代碼。 感謝幫助。

`import sys, win32com.client, datetime
# Connect with MS Outlook - must be open.
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace
("MAPI")
# connect to Sent Items
s = outlook.GetDefaultFolder(5).Items   # "5" refers to the sent item of a 
folder
#s.Sort("s", true)
# Get yesterdays date for the purpose of getting emails from this date
d = (datetime.date.today() - datetime.timedelta (days=1)).strftime("%d-%m-%
y")
# get the email/s
msg = s.GetLast()
# Loop through emails
while msg:
    # Get email date 
    date = msg.SentOn.strftime("%d-%m-%y")
    # Get Subject Line of email
    sjl = msg.Subject
    # Set the critera for whats wanted                       
    if d == date and msg.Subject.startswith("xx") or msg.Subject.startswith
    ("yy"):
    print("Subject:     " + sjl + "     Date : ", date) 
    msg = s.GetPrevious() `

這有效。 但是如果找不到根據約束的消息,它就不會退出。 我試過休息,只找到一條消息,而不是全部,我想知道是否以及如何做一個例外? 或者,如果我嘗試其他d!=日期,它也可以工作(它不會找到任何東西)。 我看不到For循環可以使用帶有msg(字符串)的日期。 我不確定 - 這里的biginner :) ??

COM API文檔相當透徹,您可以在此處查看類列表。 它還記錄了您可以用來操縱它擁有的對象的各種方法。 在您的特定示例中,您所追求的是通過日期限制您的項目集。 您將在此處的items類中看到已有一個函數。 方便地稱它為Restrict。 我能看到的唯一問題就是你需要在字符串形式中指定你想要的過濾器,因此需要你自己構建字符串。

例如,繼續您的代碼並按時間限制:

#first create the string filter, here you would like to filter on sent time
#assuming you wanted emails after 5 pm as an example and your date d from the code above
sFilter = "[SentOn] > '{0} 5:00 PM'".format(d)
#then simply retrieve your restricted items
filteredEmails = s.Restrict(sFilter)

您當然可以通過各種標准進行限制,只需查看該功能的文檔即可。 這樣,如果您限制並返回一組空項,則可以在代碼中處理該情況,而不必使用異常。 例如:

#you have restricted your selection now want to check if you have anything
if filteredEmails.Count == 0:
    #handle this situation however you would like

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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