Q:

add data to html variable in python

html file
TEMPLATE2 = """
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <br />
    <table width="auto">
        <tr>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr1  </span><br /> </td>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr2</span><br /> </td>
        </tr>
        <tr> <td>1stRow:</td><td>{1strowVal}</td></tr>
        ...
        ...
        ...
        <tr><td>25throw</td><td>{25throwVal}</td></tr>
    </table>
</body>
</html>
"""

By using above template, i have an other method with for loop, i tried many type of examples but didn't work. in my case i need to assign one variable at a time not by all at once.
tried below ways didn't work
try 1
TEMPLATE2.replace(NthrowVal, str(0))

try2
s = Template(TEMPLATE2).safe_substitute(NthrowVal="Alex")

try3
  msg = MIMEText(
       Environment().from_string(TEMPLATE2).render(
      NthrowVal="someval"
       ), "html"
    )

Any Suggestions

A:

Python variable names cannot start with an integer so you will need to change 1strowVal to be something more like rowVal1. Next, use a format string to inject the variables into your string:
rowVal1 = "SOME_STRING_VALUE"
rowVal25 = "SOME_STRING_VALUE"

TEMPLATE2 = """
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <style>
        table, th, td {
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <br />
    <table width="auto">
        <tr>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr1  </span><br /> </td>
            <td align="center"> <span style="font-size:20px; color:blue;font-weight:500">   Hdr2</span><br /> </td>
        </tr>
        <tr> <td>1stRow:</td><td>%s</td></tr>
        ...
        ...
        ...
        <tr><td>25throw</td><td>%s</td></tr>
    </table>
</body>
</html>
""" % (rowVal1, rowVal25)

