win32com Outlook滤波器按日期停止工作
我有一个脚本,显示了来自某些日期的电子邮件。它一直在工作到4月29日,现在,它无法正确过滤...
import win32com.client
import os
from datetime import datetime, timedelta
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace('MAPI')
messages = mapi.Folders("[email protected]").Folders("Inbox").Items
today = datetime.today()
start_time = today.replace(month=4,day=20, hour=0, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
end_time = today.replace(month=5,day=2,hour=20, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
messages = messages.Restrict("[ReceivedTime] >= '" + start_time
+ "' And [ReceivedTime] <= '" + end_time + "'")
messages.Sort("[ReceivedTime]", Descending=True)
for message in list(messages)[:5]:
print(message.Subject, message.ReceivedTime, message.SenderEmailAddress)
我正在使用 https://www.codeforests.com/2021/05/16/python-reading-email-email-email-email-from-utlook-2/ 作为教程。
有什么想法为什么它不按预期工作? 脚本现在根本不显示任何消息。
I have a script, that shows email's from some range of dates. It was working till 29 of april, and now, it doesn't filter correctly...
import win32com.client
import os
from datetime import datetime, timedelta
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace('MAPI')
messages = mapi.Folders("[email protected]").Folders("Inbox").Items
today = datetime.today()
start_time = today.replace(month=4,day=20, hour=0, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
end_time = today.replace(month=5,day=2,hour=20, minute=0, second=0).strftime('%Y-%m-%d %H:%M %p')
messages = messages.Restrict("[ReceivedTime] >= '" + start_time
+ "' And [ReceivedTime] <= '" + end_time + "'")
messages.Sort("[ReceivedTime]", Descending=True)
for message in list(messages)[:5]:
print(message.Subject, message.ReceivedTime, message.SenderEmailAddress)
I was using https://www.codeforests.com/2021/05/16/python-reading-email-from-outlook-2/ as a tutorial.
Any ideas why it's not working as intended?
Script doesn't show any messages at all right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保将日期格式正确。尽管日期和时间通常使用
日期
格式存储,但查找
和限制
方法要求将日期和时间转换为字符串表示。为确保按照Microsoft Outlook的期望,将日期格式化,请使用格式
函数。例如,一个vba宏显示如何使用格式
函数:在应用过滤器之前,
sort
集合也很有意义。在以下文章中阅读有关
查找
/findnext
findnext 限制方法的更多信息:Make sure that dates are formatted properly. Although dates and times are typically stored with a
Date
format, theFind
andRestrict
methods require that the date and time be converted to a string representation. To make sure that the date is formatted as Microsoft Outlook expects, use theFormat
function. For example, a VBA macro which shows how to use theFormat
function:Also it makes sense to
Sort
the collection before applying filters.Read more about the
Find
/FindNext
orRestrict
methods in the following articles: