2016-06-20 181 views
2

我使用Python 3. 我試圖按日期提取(列表/打印顯示)Outlook電子郵件。在Python中按特定日期列出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) 

回答

1

編輯:前景API有一個方法,Items.Find,用於搜索的.Items內容。如果這是你想要做的程度,那麼你可能應該這樣做。如果還有更多你想做的事,我的原創文章應該仍然有效。


現在好像你的if語句檢查集電子郵件是否等於昨天

微軟的文檔說.Items正在恢復,你首先必須通過使用幾種不同的方法,包括Items.GetNext或引用特定指數與Items.Item迭代電子郵件的集合。

然後您可以利用目前的電子郵件和訪問.SentOn property.

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

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

如果你不想迭代每條消息,你也可能嵌套兩條循環,它們回到消息中,直到它到達昨天,迭代直到它不再在「昨天」之內,然後中斷。

+0

我看到有一個地方也有Restrict方法。我無法找到它。它就像郵件items.restrict(在這種情況下指定日期y),但我看到的例子不是我第一次看到的。 (即時通訊新的python和沒有編碼爲6yrs。它只是一個人項目:) – Andy

1

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

`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循環將使用帶有味精(字符串)的日期。 我不確定 - 這裏是biginner :) ??

0

COM API文檔相當詳盡,您可以看到類列表,例如here。它還記錄了您可以用來操縱它所擁有的對象的各種方法。在你的具體例子中,你所要做的就是通過日期限制你的一組物品。您會看到項目類here中已經有一個函數。方便地稱爲限制。我可以看到這個函數的唯一缺點是,你需要指定你想要的字符串形式的過濾器,因此需要你自己構造字符串。

因此,例如,繼續你的代碼和時間限制:

#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 
相關問題