简体   繁体   中英

I would like to insert a html from result of pandas into an entire html

I hope someone will be able to help me form following issue that I am facing.

I just would like to insert a html from result of pandas into an entire html as below,

...
df_add_html=df_add.to_html()
template="""<html>
                  <head></head>
                  <body>
                  Pandas : left-only<br>
                  I want to insert df_add_html here
                  </body>
            </html>"""
part1=MIMEText(template. 'html')
msg.attach(part1)
...

The purpose is that I want to merge many results from pandas to a html and I will send a email with the html.

I hope I would get some advice from you.

Thanks.

You can use standard format()

template = "text {} text".format(df_add_html) 

or f-string

template =  f"text {df_add_html} text"

template = """<html>
                  <head></head>
                  <body>
                  Pandas : left-only<br>
                  {}
                  </body>
            </html>""".format(df_add_html)

or

template = f"""<html>
                  <head></head>
                  <body>
                  Pandas : left-only<br>
                  {df_add_html}
                  </body>
            </html>"""

BTW: http://pyformat.info


If you need more complex templates (ie. with for -loop or if/else ) then you can use jinja which is used to generate HTML in Flask (but you can generate any text file)

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