目前我使用这个解决方案Send table as an email body (not attachment ) in Python通过Python在电子邮件中发送表:
import smtplib
from smtplib import SMTPException
import csv
from tabulate import tabulate
text = """
Hello, Friend.
Here is your data:
{table}
Regards,
Me"""
html = """
<html><body><p>Hello, Friend.</p>
<p>Here is your data:</p>
{table}
<p>Regards,</p>
<p>Me</p>
</body></html>
"""
with open('result.csv') as input_file:
reader = csv.reader(input_file)
data = list(reader)
text = text.format(table=tabulate(data, headers="firstrow", tablefmt="grid"))
html = html.format(table=tabulate(data, headers="firstrow", tablefmt="html"))
message = MIMEMultipart(
"alternative", None, [MIMEText(text), MIMEText(html,'html')])
message['Subject'] = "Your data"
message['From'] = 'a@abc.com'
message['To'] = 'b@abc.com'
sender = "a@abc.com"
receivers = ['b@abc.com']
try:
smtp_obj = smtplib.SMTP('mail.abc.com')
smtp_obj.sendmail(sender, receivers, message.as_string())
print ("Successfully sent email")
except SMTPException:
print ("Error: unable to send email")
数据从csv文件加载。但我需要在表中添加边框,使其看起来像熊猫数据帧。
下面是创建带边框表的完整解决方案。
将样式添加到原始html脚本将起作用。
试试这个
相关问题 更多 >
编程相关推荐