【发布时间】:2021-05-19 19:33:30
【问题描述】:
我正在尝试运行一个代码,该代码使用主题行在我的 Outlook 中定位特定电子邮件,然后提取收到的日期/时间。当我运行下面的代码时,它按预期工作。
import win32com.client
import os
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox = mapi.GetDefaultFolder(6).
messages = inbox.Items
messages = messages.Restrict("[Subject] = 'Attached is the report for 20210511'")
for message in list(messages):
print(message.ReceivedTime)
但是,如果我想在主题中有一个变量,我无法让代码工作。我一直没有成功地尝试以下方法:
Date='20210511'
messages = messages.Restrict("[Subject] = 'Attached is the report for {Date}'")
有没有办法在主题行中获取变量?
【问题讨论】:
-
你很亲密。 f-string 语法需要
f"..."前缀,所以在你的情况下它是f"[Subject] = 'Attached is the report for {Date}'" -
完美运行,谢谢!
标签: python email variables subject