如何使用 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 循环?像您尝试做的那样从头到尾浏览您的消息。
for message in messages:
if message.Unread == True:
print (message.body)
我同意 cole 的观点,for 循环非常适合遍历所有这些。如果从最近收到的电子邮件开始很重要(例如,对于特定订单,或限制您浏览的电子邮件数量),您可以使用 Sort function to sort them by the Received Time 属性.
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)
我想阅读我的 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 循环?像您尝试做的那样从头到尾浏览您的消息。
for message in messages:
if message.Unread == True:
print (message.body)
我同意 cole 的观点,for 循环非常适合遍历所有这些。如果从最近收到的电子邮件开始很重要(例如,对于特定订单,或限制您浏览的电子邮件数量),您可以使用 Sort function to sort them by the Received Time 属性.
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)