【问题标题】:How to send a Python Dataframe through an E-mail?如何通过电子邮件发送 Python Dataframe?
【发布时间】:2021-10-11 15:30:58
【问题描述】:

我无法通过电子邮件发送我的 DataFrame。每当我导出或打印它时,DataFrame 看起来都很好。然而,通过电子邮件,它看起来一团糟。

当然,我已经尝试过 Pandas 提供的 to_html 选项。还有来自pretty_html_table 包的build_table

每当我通过电子邮件发送数据帧时,它都会返回:


Dear mister X,

Please have a look at these numbers. There is a big change in revenue since 11-10-2021 compared to 10-10-2021:

 <p><table border="0" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Product Category</th>
      <th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Source / Medium</th>
      <th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Avg. Rev. Period 1</th>
      <th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Avg. Rev. Period 2</th>
      <th style = "background-color: #FFFFFF;font-family: Century Gothic, sans-serif;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px;width: auto">Percentage Change</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">TEST</td>
      <td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">TEST</td>
      <td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">0.01</td>
      <td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">100</td>
      <td style = "background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto">-52</td>
    </tr>
.........

我认为这可能与我用来发送电子邮件的代码有关。我想在其中插入不同的变量。这是电子邮件的代码:


email_df = build_table(df, "blue_light")
subject = f"Subject: blablabla - {today}"
message = f"\n\nDear mister X, \n\n Please have a look at these numbers. There is a big change in revenue since {period 1} compared to {period 2}: \n\n {email_df}"
emailcontent = f'{subject}{message}'

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, emailcontent)

欢迎任何帮助! :)

【问题讨论】:

  • 您似乎将 HTML 表格插入到纯文本电子邮件中。您是否尝试过直接插入dfprint("... ... \n{df}") 这将提供一个纯文本表格。
  • 我试过直接添加DF。这种布局没有意义。列和行是正确的,但间距到处都是。如何在整个 HTML 电子邮件中添加 DF?

标签: python pandas dataframe email


【解决方案1】:

您当前以纯文本格式发送电子邮件,因此无法确保列间距正确,除非收件人碰巧在纯文本阅读器中查看他们的电子邮件。

为确保表格间距正确,您必须发送 HTML 电子邮件 - 请参阅 example herethis answer。类似以下(未经测试)的东西应该可以工作:

from email.message import EmailMessage
import smptlib

email = EmailMessage()
email['Subject'] = f"Subject: blablabla - {today}"
email['From'] = sender_email
email['To'] = receiver_email
email.set_content(f"""\
    <html><head></head><body>
    <p>Dear mister X,</p>
    <p>Please have a look at these numbers. There is a big change in revenue since {period 1} 
    compared to {period 2}:</p>
    {df.to_html()}   # or use build_table(df) for prettier formatting
    </body></html>""", subtype='html')

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.send_message(email)

【讨论】:

  • 是的,这更多的是它。我不得不重新构建我的脚本,但它起作用了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 2016-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
相关资源
最近更新 更多