Search code examples
pythonhtmlhtml-tableoutlookoutlook-api

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 protected]"

What can I do? Thank you in advance


Solution

  • 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"