在Python中按特定日期列出Outlook电子邮件

2024-04-28 10:45:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用Python 3。 我正在尝试按日期提取(列表/打印显示)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)

Tags: toclient列表getdatetimedate电子邮件items
3条回答

COM API文档相当全面,您可以看到类列表,例如here。它还记录了可以用来操作它拥有的对象的各种方法。在您的特定示例中,您所追求的是通过日期限制您的项集。在items类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

我完成了密码。谢谢你的帮助。

`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循环将使用带有msg(字符串)的日期。 我不确定——这里是比金纳:) ??

outlook API有一个方法^{},用于搜索.Items的内容。如果这是你想做的事情的范围,那可能就是你应该做的。


现在,您的if语句似乎正在检查一组电子邮件是否等于昨天的电子邮件。

微软的文档说,.Items返回一个电子邮件集合,您首先必须使用几个不同的方法(包括Items.GetNext)或通过使用Items.Item引用一个特定的索引来遍历它。

然后您可以接收当前电子邮件并访问^{} property.

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

这应该遍历sent文件夹中的所有消息,直到sent.GetNext()没有更多的消息可返回。您必须确保y的格式与.SentOn返回的格式相同。

如果您不想遍历每个消息,您可能还可以嵌套两个循环,这两个循环返回到消息中,直到它到达昨天,迭代到不再在“昨天”内,然后中断。

相关问题 更多 >