10

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?

2
  • Look into ipyhon notebooks, very handy for this type of work Commented Oct 30, 2013 at 10:36
  • @Woody Pride: Not with ipython notebook :) Commented Oct 30, 2013 at 10:37

2 Answers 2

13

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())
Sign up to request clarification or add additional context in comments.

Comments

4

Finally found. This is the way it should be done.

filename = "test.html"
f = file(filename)
attachment = MIMEText(f.read(),'html')
msg.attach(attachment)

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.