I am trying to send outlook mail via python. I import some data from excel files using dataframe and after some filtering I send them where the body of the mail is:
body = '<html><body>'+df.to_html()+'</body></html>'
I get a table but the cells are not aligned.I can not use justify='left/write' as I want the cells to be center aligned,not just the column headers. I have used styles but it did not work either:
df.style.set_properties(subset=df.columns,**{'width':'10em', 'text-align':'center'})\
I tried this too and it did not work.
p=HTML(df.to_html(classes= 'table text-align:center'))
After surfing through similar problems I found another solution:
s = df.style.set_properties(**{'text-align': 'center'})
s.render()
However it makes the border of the table disappear.So I modified it:
s = df.style.set_properties(**{'text-align': 'center','border-color':'Black','border-width':'thin','border-style':'dotted'})
This gives border to the cells. But it looks like each cell is inside individual textbox,not like a table. How do I get this done?
The final result looks like this:

You need to style td and th elements of your table.
style_td = dict(selector="td", props=[('border-collapse', 'collapse')])
style_th = dict(selector="th", props=[('border-collapse', 'collapse')])
After that you can put this dicts to your dataframe.style.set_table_styles part. Like;
df_styled = df.style.set_table_styles([style_td, style_th])
It works for me. And also you can add more CSS option props to your each HTML element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With