Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send pandas dataframe data as html e-mail

I want to send a pandas dataframe data as an HTML e-mail. Based on this post I could create an html with the dataframe. Code

import pandas as pd
import numpy as np

HEADER = '''
<html>
    <head>

    </head>
    <body>
'''
FOOTER = '''
    </body>
</html>
'''

df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD')]).T
with open('test.html', 'w') as f:
    f.write(HEADER)
    f.write(df.to_html(classes='df'))
    f.write(FOOTER)

Now I want to send this as a html e-mail. I tried this. Can not figure out how to attach the html file?

like image 624
Nilani Algiriyage Avatar asked Oct 30 '13 10:10

Nilani Algiriyage


1 Answers

Pandas has a function for this.

This will give the the html code for the table, after which you can embed it into an email with:

df = DataFrame(data)

email = " some html {df} lah lah"

email = email.format(df=df.to_html())
like image 166
Johnny V Avatar answered Oct 19 '22 05:10

Johnny V