Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python按相反顺序浏览Outlook电子邮件_Python_Win32com - Fatal编程技术网

如何使用Python按相反顺序浏览Outlook电子邮件

如何使用Python按相反顺序浏览Outlook电子邮件,python,win32com,Python,Win32com,我想阅读我的Outlook电子邮件,只想阅读未读的邮件。我现在掌握的代码是: import win32com.client outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") inbox = outlook.GetDefaultFolder(6) messages = inbox.Items message = messages.GetFirst () while message:

我想阅读我的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)

我同意科尔的观点,一个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)

这不就是改变message=messages.GetFirst()吗?GetLast()如果存在,或者寻找一个函数来执行类似的操作是的,有一个
GetLast
和一个
GetPrevious
方法。如何以相反的顺序获取它们应该是不言而喻的…
GetLast()
GetNext()
不能一起工作@OmidCompSCI,我找不到
GetPrevious()
。谢谢你@kindall