简体   繁体   English

Python-将Pandas数据框写入电子邮件正文

[英]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. 我正在尝试将Pandas数据框的内容导出到电子邮件正文中。 I tried to use the pandas.DataFrame.to_html method to generate the relevant html code for the table, but this hasn't worked. 我试图使用pandas.DataFrame.to_html方法为表生成相关的html代码,但这没有用。

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'. 简而言之,错误消息是“ data_html”是“方法”而不是字符串,因此无法串联形成“ 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. PS我正在通过Anaconda发行版使用Python 3.7.2。

to_html is a method, so you need to call it like this df_events.to_html() . to_html是一种方法,因此您需要像df_events.to_html()这样调用它。 See more details here https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.to_html.html 在此处查看更多详细信息https://pandas.pydata.org/pandas-docs/version/0.23/genic/pandas.DataFrame.to_html.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM