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

4
我正在使用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)
3个回答

4

我已经完成了代码。感谢您的帮助。

`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() `

这段代码是有效的。但是如果没有符合约束条件的消息,它就不会退出。我尝试使用break语句,但只能找到一个消息而不是所有消息。我在想是否可以使用异常来解决这个问题?或者如果我尝试使用else d != date,它也不起作用(它将找不到任何内容)。 我无法看出如何使用日期和消息(字符串)进行For循环。 我不确定 - 我是初学者 :)


3
Outlook API有一种方法Items.Find,可用于搜索.Items的内容。如果您只想做这些,则应该使用此方法。
目前,似乎您的if语句正在检查电子邮件集是否等于昨天
Microsoft的文档中指出,.Items返回的是一个电子邮件的集合,您需要使用几种不同的方法进行迭代,包括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返回的格式相同。
如果您不想迭代每个消息,您可能还可以嵌套两个循环,回到消息中直到昨天,迭代直到不再处于“昨天”之内,然后退出。

我在某处看到了一个 Restrict 方法。但我再也找不到它了。它的用法是:如果邮件项.restrict(在这种情况下指定日期为y),但我看到的示例与我最初看到的不同。(我是 Python 的新手,已经有6年没有编程了。这只是一个个人项目 :) - Andy

1
COM API文档非常详细,你可以在这里查看类列表here。它还记录了您可以使用的各种方法来操作它拥有的对象。在您的特定示例中,您需要通过日期限制项目集。您会发现,在items类here中已经有一个用于此目的的函数。方便的是,它被称为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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接