簡體   English   中英

如何使用Python以相反的順序瀏覽Outlook電子郵件

[英]How to go through Outlook emails in reverse order using Python

我想閱讀我的Outlook電子郵件,僅閱讀未讀的電子郵件。 我現在擁有的代碼是:

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetFirst ()
while message:
    if message.Unread == True:
        print (message.body)
        message = messages.GetNext ()

但這是從第一封電子郵件到最后一封電子郵件。 我想按相反的順序進行,因為未讀電子郵件將排在最前面。 有沒有辦法做到這一點?

我同意科爾的觀點,for循環很適合遍歷所有這些。 如果從最近收到的電子郵件開始很重要(例如,對於特定順序或限制您瀏覽的電子郵件數量),則可以使用“ 排序”功能按“ 接收時間”屬性對它們進行排序。

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
#the Sort function will sort your messages by their ReceivedTime property, from the most recently received to the oldest.
#If you use False instead of True, it will sort in the opposite direction: ascending order, from the oldest to the most recent.
messages.Sort("[ReceivedTime]", True)

for message in messages:
     if message.Unread == True:
         print (message.body)

為什么不使用for循環? 要像看起來一樣從頭到尾瀏覽您的消息,您正在嘗試這樣做。

for message in messages:
     if message.Unread == True:
         print (message.body)

暫無
暫無

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

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