【发布时间】:2022-01-17 07:29:44
【问题描述】:
每个星期一我都会收到主题标题稍有改变的附件。主题标题的固定部分是PB Report,日期为星期一。例如,我在这个星期一收到了主题为 PB Report - 13.12.2021、上周 PB Report - 06.12.2021 等的电子邮件。我想在此代码中实现GetLast,以便仅获取最新发送的报告。另外,我如何告诉 python 搜索以 PB Report 开头的主题,不要看标题的其余部分。我尝试将通配符(*)设为save_attachments('PB Report*'),但没有成功。
import datetime
import os
import win32com.client
path = r"C:/Users/greencolor/Desktop/Autoreport/"
today = datetime.date.today()
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
def save_attachments(subject):
for message in messages:
if message.Subject == subject:
for attachment in message.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(path, str(attachment)))
if __name__ == "__main__":
save_attachments('PB Report - 13.12.2021')
我也有替代代码,但是当我运行此代码时,我从未得到结果或错误。需要。
import os
import win32com.client
path = r"C:/Users/greencolor/Desktop/Autoreport/"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.GetLast()
while message:
if 'PB' in message.subject and 'Report' in message.subject:
for attachment in message.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(path, str(attachment)))
【问题讨论】:
-
您能否更详细地解释一下:您的主要问题是什么(在 3 个问题中)?您的代码当前存在什么问题?请描述什么不起作用、预期什么以及实际输出/行为是什么。
-
主要问题是如何通过主题名称搜索从 Outlook 下载多个附件(附加在最新电子邮件中)(但 50% 的主题总是在变化)。