0

I was able to create two dataframe lists and translate them into HTML, but I would like to know how to include them in order so they will populate my email I intend to send each day. I have updated my code and question on this post. For each {0), {1}, etc I would like to insert a dataframe. Is that possible?

import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import pandas as pd
import matplotlib

 ALL_DFS = [L_MUNIC_s, L_MUNIF_s, L_CAMNY_s, L_NYMNY_s]
 ALL_DFS_html= ''
 for ALL_DF in ALL_DFS:
     ALL_DFS_html += ALL_DF.render()

 ALL_DFS2 = [L_MUNIC_NAV, L_MUNIF_NAV, L_MUNIC5, L_MUNIF5]
 ALL_DFS_html2= ''
 for ALL_DF2 in ALL_DFS2:
     ALL_DFS_html2 += ALL_DF2.to_html()


def send_mail():
     dataf1 = ALL_DF
     dataf2 = ALL_DF2
     sender = "<name>@gmail.com"
     receiver = ['<name>@gmail.com']
     msg = MIMEMultipart('related')

today = datetime.date.today()
msg['Subject'] = "Daily Email " + 
str(today.strftime("%m/%d/%y"))
msg['From'] = sender
msg['To'] = ", ".join(receiver)
html = """\
<html>
  <head></head>
  <body>
    <p>Good Morning Team!<br>
       L-MUC
       NET ASK: {0}
       Exposure:<br>
       {1}
       <br>Top 5:<br>
       {2}
       <br>
       <br>NEW:<br>
       {3}
       <br>
    </p>
    <p>
       L-MUF
       NET ASK: {4}
       Exposure:<br>
       {5}
       <br>Top 5:<br>
       {6}
       <br>
       <br>NEW:<br>
       {7}
       <br>
    </p>
    <p>
       L-CAY
       NEW ASK: {8}
       Exposure:<br>
       {9}
       <br>NEW:<br>
       {10}
       <br>
    </p>
    <p>
       L-NYY
       NEW ASK: {11}
       Exposure:<br>
       {12}
       <br>NEW:<br>
       {13}
       <br>
    </p>
  </body>
</html>

""".format(dataf1.render(), dataf2.to_html())

partHTML = MIMEText(html, 'html')
msg.attach(partHTML)
ser = smtplib.SMTP('gateway_server', port_number)
ser.login("username", "password")
ser.sendmail(sender, receiver, msg.as_string())
return send_mail()
5
  • Does this answer your question? Python How to send multiple dataframe as HTML tables in the email body Commented Jul 9, 2021 at 9:45
  • The question provided answers how to do this with the tables in the email body. Your code seems to attempt to put them in attachments, but from what you write, it seems that your are indeed trying to put them in the body. Could you clarify this? Hope it helps. Commented Jul 9, 2021 at 9:47
  • Will take a look after diving into the below response, thank you! Commented Jul 9, 2021 at 10:57
  • The email code here is rather clumsy, and seems to be based on an earlier Python version. The email library was overhauled in Python 3.6; perhaps throw out the legacy code and start over based on the examples in the current documentation. Commented Jul 9, 2021 at 12:54
  • Unfortunately I cannot update to Python 3.6, I have to use Python 3.0. With that being said do you have any advice for the email code and how to make it less clumsy? Thanks! Commented Jul 11, 2021 at 20:55

1 Answer 1

0

If the link provided in the comments doesn't answer your question, I wonder if this could solve your problems.

# Let's assume you have some list of dataframes.
dfs = [pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) for _ in range(4)]

dfs_html = ''
for df in dfs:
    dfs_html += df.to_html()

dfs_html is now a nicely formatted bit of html with all your data.

Data as nicely formatted html tables

Sign up to request clarification or add additional context in comments.

4 Comments

What if my Dataframes were multi-indexed and i manipulated them so they are already defined as L_MUC_s1, L_MUC5_2, L_MUF_s1,L_MUF5_2. Could I still pass them into the list you created?
I ran the below and received an error: TypeError: __init__() takes from 1 to 6 positional arguments but 7 were given. ALL_DFS = [pd.DataFrame(L_MUNIC_s1, L_MUNIF_s1, L_CAMNY_s1, L_NYMNY_s1, L_MUNIC5_2, L_MUNIF5_2) for _ in range(5)] ALL_DFS_html= '' for ALL_DF in ALL_DFS: ALL_DFS_html += ALL_DF.to_html()
It should be ALL_DFS = [L_MUNIC_s1, L_MUNIF_s1, L_CAMNY_s1, L_NYMNY_s1, L_MUNIC5_2, L_MUNIF5_2)]
I made the edit and now I am getting an error: AttributeError: 'str' object has no attribute 'to_html'. Specifically for the last line of code ALL_DFS_html += ALL_DF.to_html()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.