繁体   English   中英

如何通过电子邮件发送 Python 数据框?

[英]How to send a Python Dataframe through an E-mail?

我在通过电子邮件发送 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 电子邮件 - 请参阅此处示例此答案 类似以下(未经测试)的东西应该可以工作:

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)

暂无
暂无

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

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