Created
March 17, 2023 15:12
-
-
Save iKunalChhabra/5f8fd50d7fcb8bdcd8be7102c8038a31 to your computer and use it in GitHub Desktop.
Create excel like table html from pandas dataframe to send in email reports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def df_to_html_table(df): | |
css = """ | |
<style> | |
table { | |
border-collapse: collapse; | |
width: 50%; | |
margin: 20px auto; | |
font-family: Arial, sans-serif; | |
} | |
th { | |
background-color: #4f81bd; | |
color: white; | |
border: 1px solid white; | |
padding: 8px; | |
text-align: left; | |
text-align: center; | |
} | |
td { | |
border: 1px solid #4f81bd; | |
padding: 8px; | |
text-align: left; | |
} | |
tr:nth-child(even) { | |
background-color: #d3e5f2; | |
} | |
tr:hover { | |
background-color: #c4d4e8; | |
} | |
</style> | |
""" | |
html_string = css + df.to_html(index=False) | |
return html_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment