admin管理员组

文章数量:1295873

I am converting a pandas dataframe to HTML table but unable to figure out why the HTML table does not retain the conditional format. When I save the dataframe to excel, i do see the formatting as expected. Any insight is appreciated. Thank You.

fiveg = pd.read_csv('5g.csv', engine="python", encoding="UTF-8")

   def highlights(row):
      if row['Col A'] < 1:
         return ['background-color: yellow'] * len(row)
      else:
         return [''] * len(row)


fiveg_style = fiveg.style.apply(highlights, axis=1)
## The appropriate rows are highlighted in yellow
fiveg_style.to_excel('test.xlsx', engine='openpyxl', index=False)
# the html table is created but the appropriate rows are NOT higlighted.
html_table_fiveg = fiveg_style.to_html(index=False)



FROM = 'XXX'
TO = "YYY"
SUBJECT = "ABC"

styled_HTML = f"""\
<html>
<head>
   <style>
      table {{
            width: 100%;
            border-collapse: collapse;
            font-size: 10px;
            }}
        th, td {{
            border: 1px solid black;
            padding: 8px;
            text-align: justify;
               }}
        th {{
            background-color: #F0F8FF;
            }}
   </style>
</head>
<body>
   <p><b>5G</b><br>
   {html_table_fiveg}
   </p>
</body>
</html>
"""

本文标签: Pandas dataframe to HTML table (Gmail Body)Stack Overflow