[英]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.