HTML and Python : How to make variables in a html code written in a python script
Question: 
    from bs4 import BeautifulSoup
    import os
    import re
    
    htmlDoc="""
    <html>
    <body>
    
    <table class="details" border="1" cellpadding="5" cellspacing="2" style="width:95%">
      <tr>
        <td>Roll No.</td>
        <td><b>Subject 1</b></td>       
        <td>Subject 2</td>
      </tr>
      <tr>
        <td>01</td>
        <td>Absent</td>     
        <td>Present</td>
      </tr>
      <tr>
        <td>02</td>
        <td>Absent</td>     
        <td>Absent</td>
      </tr>
    </table>
    
    </body>
    </html>
    """
    soup = BeautifulSoup(htmlDoc,"lxml")
    
    #table = soup.find("table",attrs={class:"details"})
    
    
    
    html = soup.prettify("utf-8")
    with open("/home/alan/html_/output.html", "wb") as file:
        file.write(html)
    

I've used BeautifulSoup to write the HTML code. In the code, the variable
I'vve to make is Present, Absent. Upon change of some parameter, I've to
change the values, change present to absent and vice versa. I've to make
pesent/absent a variable 'a'.


Answer: Do you mean composing your own html from some form on python data using
BeautifulSoup? If so, the example below may be useful to you:

    
    
    from bs4 import BeautifulSoup, Tag
    import os
    
    subjects = ['subject1', 'subject2']
    vals = [['absent','present'],['absent','absent']] #rows
    
    titles = ['Roll No.'] + subjects
    
    html = """<html>
                  <body>
                      <table class="details" border="1" cellpadding="5" cellspacing="2" style="width:95%">
                      </table>
                  </body>
              </html>"""
    soup = BeautifulSoup(html)
    
    #find table tag
    table = soup.find('table')
    
    #add header to table tag
    tr = Tag(table, name = 'tr')
    for title in titles:
        td = Tag(tr, name = 'td')
        td.insert(0, title)
        tr.append(td)
    table.append(tr)
    
    #add data to table one row at a time
    for i in range(len(vals[0])):
    
        tr = Tag(table, name = 'tr')
    
        td = Tag(tr, name = 'td')
        td.string = str(i+1)
        tr.append(td)
    
        for val in vals[i]:
            td = Tag(tr, name = 'td')
            td.string = val
            tr.append(td)
    
        table.append(tr)
    
    os.chdir(os.getcwd())
    f = open('test.html','w')
    f.write(soup.prettify())
    f.close()
    

