如何使用 python 格式化 html table
How to format an html table using python
我正在尝试在我的 python 脚本中格式化 html table,然后将其作为电子邮件发送。 table 源自 pandas 数据帧,然后我使用 .to_html()
函数将其保存到 HTML。
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
from smtplib import SMTProm email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
from smtplib import SMTP
# Save dataframe to html
df_final.to_html(r'Data/trend_report_word_3m_rolling.html',col_space = 20, index = False)
#read html
report_file = open(r'Data\trend_report_word_3m_rolling.html')
html_excel = report_file.read()
# Prepare to send email
part_html = MIMEText(html_report, 'html')
#Attach attachments to message
msg.attach(part)
msg.attach(part_html)
# Send the message via SMTP server
with SMTP("") as smtp:
smtp.send_message(msg)
#print("The email was sent to: ",string_receiver)
smtp.quit()
据我所知,使用 .read()
函数只能读取 html 文件,但它不允许我编辑 html?
我最后想要的是让电子邮件的收件人看到带有格式(例如颜色和特定列宽)的 table。
也许我应该先格式化我的数据框然后将其保存到 HTML?
如果我的方法不清楚,请问我一些问题。
谢谢。
您需要一个 html 模板模块,例如:
https://opensource.com/resources/python/template-libraries
或 Mustache(它是简单的小型模板系统):
我正在尝试在我的 python 脚本中格式化 html table,然后将其作为电子邮件发送。 table 源自 pandas 数据帧,然后我使用 .to_html()
函数将其保存到 HTML。
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
from smtplib import SMTProm email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
from smtplib import SMTP
# Save dataframe to html
df_final.to_html(r'Data/trend_report_word_3m_rolling.html',col_space = 20, index = False)
#read html
report_file = open(r'Data\trend_report_word_3m_rolling.html')
html_excel = report_file.read()
# Prepare to send email
part_html = MIMEText(html_report, 'html')
#Attach attachments to message
msg.attach(part)
msg.attach(part_html)
# Send the message via SMTP server
with SMTP("") as smtp:
smtp.send_message(msg)
#print("The email was sent to: ",string_receiver)
smtp.quit()
据我所知,使用 .read()
函数只能读取 html 文件,但它不允许我编辑 html?
我最后想要的是让电子邮件的收件人看到带有格式(例如颜色和特定列宽)的 table。
也许我应该先格式化我的数据框然后将其保存到 HTML? 如果我的方法不清楚,请问我一些问题。
谢谢。
您需要一个 html 模板模块,例如:
https://opensource.com/resources/python/template-libraries
或 Mustache(它是简单的小型模板系统):