我正在尝试从我的电子邮件中获取所有附件,或者获取具有特定标题的最后一封电子邮件
附件
到目前为止,我有以下几点:
import pandas as pd
import win32com.client
import os
import datetime as date
import time
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case the inbox. You can change that number to reference
messages = inbox.Items
message = messages.GetLast()
val_date = date.date.today()
sub_today = 'GG in GD'
for item in messages:
if item.Subject == sub_today:
print(item.Subject)
print(item.SentOn)
attachments = item.Attachments
attachment = attachments.Item(1)
attachment.SaveAsFile(os.getcwd() + '\\' + str(attachment))
Good_Data = pd.read_csv(os.getcwd() + '\\' + str(attachment))
因为这个脚本的下一部分要求我在2.7上运行它,所以我不确定如何修复这个代码来获得我需要的。在3.6中,我可以这样做:
for item in messages:
if item.Subject == sub_today and item.SentOn.date() == val_date:
理想情况下,我想得到过去几天所有这些附件的列表,并抓取带有附件的最近一个,可能还有日期。
我在使用
item.SentOn
哪种类型
time
. 我试着把它转换成字符串,然后
datetime.datetime.strptime()
,或从
time.time()
试着看看它是否大于。但我没有取得任何成功。
即使我试图转换
时间
反对
datetime
message.SentOn
<PyTime:3/7/2018 10:18:03 AM>
date.date.fromtimestamp(message.SentOn)
datetime.date(1970, 1, 1)
我想让它做一些类似的事情:
for item in messages:
if item.Subject == sub_today and item.SentOn > time.time.today()-2weeks and item.Attachments is not null:
attachments = item.Attachments
attachment = attachments.Item(1)
att.append(attachment)
attachment.SaveAsFile(os.getcwd() + '\\' + str(attachment))
Good_Data = pd.read_csv(os.getcwd() + '\\' + str(attachment))