简体   繁体   中英

How can I print html text with python using variables?

I need to print an HTML text within an email using python and taking values from variables.

I tried using the following, but in the htmlbody section it returns an error, and it appears to only be working when I type everything as a string, but I need to be able to make a reference to variables

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"

What can I do? Thank you in advance

You are trying to pass a tuple and not a string. You could use the following instead:

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

Another way is to use the + sign:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM