Pandas Style Object to Outlook HTML Body

Viewed 1203

Several questions already exist on this topic but none clearly jump from df.style generation to Outlook.

The issues is: some df formatting is lost adding a styled df to an Outlook email body which is not lost saving the same df as df.html file.

Emailer Function

def Emailer(message, subject, recipient):
    import win32com.client as win32
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = recipient
    mail.Subject = subject
    mail.GetInspector
    print(mail.HTMLbody[:index + 1] + \
        message + mail.HTMLbody[index + 1:])
    mail.HTMLbody = mail.HTMLbody[:index + 1] + \
        message + mail.HTMLbody[index + 1:]

    mail.Display(True)

Dataframe styler

#format header
#format odd rows
#format hover
df_style=df.style.set_table_styles(
[{'selector': 'th',
    'props': [('background-color', '#4DAA21'),
               ('color', '#0F110D'),
               ('font-size','15')]},

 {'selector': 'tr:nth-of-type(odd)',
    'props': [('background-color', '#CBDEC3')]},
 
 {'selector': 'tr:hover',
    'props': [('background-color', '#7FE9F0')]},

]
)

Render the styled df

with open('DataFrame.html', 'w') as f:
    f.write(df_style.hide_index().render() )
    f.close

html = df_style.hide_index().render()

Call function

Emailer(html,Subject, "benjamin@ben.com")

I've tried using to_html but that is limited in formatting.

I've tried inspecting the css string that is generated but nothing was immediately apparent.

I've been back to the documentation and clearing the formatting is correct because the file is appears correctly formatted.

I think the issue is in grabbing and indexing the mail.HTMLbody.

0 Answers
Related