繁体   English   中英

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

[英]listing Outlook emails by specific date in 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)

outlook API有一个方法Items.Find ,用于搜索.Items的内容。 如果这是您想要做的程度,那可能就是您应该如何做到的。


现在看起来你的if语句正在检查一组电子邮件是否等于昨天

Microsoft的文档说.Items正在返回一组电子邮件,您首先必须使用几种不同的方法(包括Items.GetNext或通过使用Items.GetNext引用特定索引来Items.Item

然后,您可以获取当前的电子邮件并访问.SentOn属性。

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

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

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

我完成了代码。 感谢帮助。

`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(字符串)的日期。 我不确定 - 这里的biginner :) ??

COM API文档相当透彻,您可以在此处查看类列表。 它还记录了您可以用来操纵它拥有的对象的各种方法。 在您的特定示例中,您所追求的是通过日期限制您的项目集。 您将在此处的items类中看到已有一个函数。 方便地称它为Restrict。 我能看到的唯一问题就是你需要在字符串形式中指定你想要的过滤器,因此需要你自己构建字符串。

例如,继续您的代码并按时间限制:

#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM