简体   繁体   中英

Python - write Pandas data frame into body of email

I'm trying to export the contents of a Pandas data frame into the body of an email. I tried to use the pandas.DataFrame.to_html method to generate the relevant html code for the table, but this hasn't worked.

text_body = "Text"    
head_style = '<style></style>'
titles = '<h2>Upcoming events</h2>'
data_html = df_events.to_html
html_body = '<html>' + head_style + '<body>' + titles + data_html + '</body></html>'

# Add body to email
part1 = MIMEText(html_body, 'html')
part2 = MIMEText(text_body, 'plain')
message.attach(part1)
message.attach(part2)

The error message is, in a nutshell, that 'data_html' is a "method" and not a string and therefore cannot be concatenated to form 'html_body'. I am at a loss to what to do. Any ideas?

PS I am using Python 3.7.2 via the Anaconda distribution.

to_html is a method, so you need to call it like this df_events.to_html() . See more details here https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.to_html.html

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