【发布时间】: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 表格插入到纯文本电子邮件中。您是否尝试过直接插入
df?print("... ... \n{df}")这将提供一个纯文本表格。 -
我试过直接添加DF。这种布局没有意义。列和行是正确的,但间距到处都是。如何在整个 HTML 电子邮件中添加 DF?
标签: python pandas dataframe email