pandas to_html using the .style options or custom CSS?

前端 未结 3 1778
眼角桃花
眼角桃花 2020-11-29 01:10

I was following the style guide for pandas and it worked pretty well.

How can I keep these styles using the to_html command through Outlook? The documentation seems

相关标签:
3条回答
  • 2020-11-29 01:16

    Selecting the table (the rendered, styled, dataframe widgets in jupyter) and copy-pasting to an email body worked for me (using outlook office).

    No manual html extraction, saving, loading, or anything like that.

    0 讨论(0)
  • 2020-11-29 01:33

    Once you add style to your chained assignments you are operating on a Styler object. That object has a render method to get the html as a string. So in your example, you could do something like this:

    html = (
        df.style
        .format(percent)
        .applymap(color_negative_red, subset=['col1', 'col2'])
        .set_properties(**{'font-size': '9pt', 'font-family': 'Calibri'})
        .bar(subset=['col4', 'col5'], color='lightblue')
        .render()
    )
    

    Then include the html in your email instead of a df.to_html().

    0 讨论(0)
  • 2020-11-29 01:33

    It's not an extravagant / pythonic solution. I inserted the link to a direct css file before the html code made by the to_html () method, then I saved the whole string as an html file. This worked well for me.

    dphtml = r'<link rel="stylesheet" type="text/css" media="screen" href="css-table.css" />' + '\n'
    dphtml += dp.to_html()
    
    with open('datatable.html','w') as f:
        f.write(dphtml)
        f.close()
        pass
    
    0 讨论(0)
提交回复
热议问题