0

I'm writing a script to email a pandas dataframe as a table to several people. I have my dataframe frame:

     SITE  ...   VISITS
438     1  ...      104
439     2  ...      104
440     3  ...      104
504     4  ...      215

I'm trying to create a pretty_html_table that I can email using:

from pretty_html_table import build_table

html_table = build_table(frame, 'blue_light')

However, instead of emailing a table, the script emails a bunch of html text!

<p><table border="0" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th style = "background-color: #FFFFFF;font-family: Century Gothic;font-size: medium;color: #305496;text-align: left;border-bottom: 2px solid #305496;padding: 0px 20px 0px 0px">SITE</th>
etcetera, etcetera ...     

I'm trying to append this table to an email like so:

import win32com.client as win32

def report_email(email):        
    html_table = build_table(frame, 'blue_light')
    
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = email
    mail.Subject = 'Table below'
    mail.Body  = "Hi, here is a table" + html_table
    
    return mail.Send()

Thanks so much for all you help!

0

1 Answer 1

2

pretty_html_table.build_table is expected to return the HTML code for the table. The problem is that you use mail.Body. Use mail.HTMLBody instead.

Here is MailItem.HTMLBody docs

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

2 Comments

Thanks so much! This works, except now the text in my mail.Body doesn't send ... anyway to send both? Preferably the text, then the html table below
Why would you have anything in Body, if you want to send HTMLBody? Everything should be in HTMLBody. You can send mail as plain text or as HTML, not both (open Outllok and check the settings).

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.