I am trying to format an html table in my python script and then send it as an email. The table originates from a pandas dataframe which I then saved to HTML using the .to_html()
function.
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()
As far as I understand using the .read()
function only reads the html file yet it does not allow me to edit the html?
What I would like at the end is for the recipient of the email to see the table with formats (such as colors and specific column width).
Maybe I should first format my dataframe and than save it to HTML? Please ask me some questions if my method is unclear.
Thank you.
You need a html template module such as:
https://opensource.com/resources/python/template-libraries
or Mustache (it is easy and small template system):