使用Pandas的Styler对象,并将其转换为HTML格式以防止将样式作为纯文本替换值。代码示例:
import pandas as pd
# 创建示例数据
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Salary': [40000, 50000, 60000]}
df = pd.DataFrame(data)
# 使用Styler对象应用样式
styled_table = df.style.set_properties(**{'background-color': 'lightblue',
'color': 'white'})
# 将表转换为HTML格式
html_table = styled_table.render()
print(html_table)
此代码将生成一个带有基本样式的HTML表,其中每个单元格都带有背景色和文本颜色。
上一篇:ApplyingOOTBstylestoextendedcomponent
下一篇:Applyingstylertotablereplacesthevalueswithstyleasplaintext