【问题标题】:Send pandas dataframe data as html e-mail将 pandas 数据帧数据作为 html 电子邮件发送
【发布时间】:2013-11-09 21:11:30
【问题描述】:

我想将 pandas 数据帧数据作为 HTML 电子邮件发送。基于this 帖子,我可以使用数据框创建一个html。代码

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)

现在我想将其作为 html 电子邮件发送。我试过this。无法弄清楚如何附加 html 文件?

【问题讨论】:

  • 查看 ipyhon 笔记本,非常适合这类工作
  • @Woody Pride:不适用于 ipython notebook :)

标签: python html email pandas


【解决方案1】:

Pandas 有一个函数for this

这将为表格提供 html 代码,之后您可以将其嵌入到电子邮件中:

df = DataFrame(data)

email = " some html {df} lah lah"

email = email.format(df=df.to_html())

【讨论】:

    【解决方案2】:

    终于找到了。应该这样做。

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2020-05-08
    • 1970-01-01
    相关资源
    最近更新 更多