【问题标题】:Adding a variable in an email subject在电子邮件主题中添加变量
【发布时间】: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


【解决方案1】:

你想使用 f 字符串。

Date='20210511'
messages = messages.Restrict(f"[Subject] = 'Attached is the report for {Date}'")

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 2014-06-26
    • 2012-06-03
    • 2014-01-16
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-17
    相关资源
    最近更新 更多