如何使用变量打印带有 python 的 html 文本?

How can I print html text with python using variables?

我需要使用 python 并从变量中获取值来在电子邮件中打印 HTML 文本。

我尝试使用以下内容,但在 htmlbody 部分它 returns 一个错误,并且它似乎只有在我将所有内容都输入为字符串时才有效,但我需要能够进行参考到变量

import win32com.client
from win32com.client import Dispatch, constants
testo="Edoardo!!!"
const=win32com.client.constants
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = "Test !!"
newMail.HTMLBody = ("Some text here<u>",---variable here---,"</u>Othertext")
newMail.To = "email@demo.com"

我能做什么? 提前谢谢你

您正在尝试传递元组而不是字符串。您可以改用以下内容:

newmail.HTMLBody = "Some text here<u>{var}</u>Othertext".format(var=a)

另一种方法是使用+号:

newmail.HTMLBody = "Some text here<u>" + a + "Othertext"