【问题标题】:Pandas dataframe.to_html() - edit text color and add background color of header columnsPandas dataframe.to_html() - 编辑文本颜色并添加标题列的背景颜色
【发布时间】:2021-03-02 08:59:18
【问题描述】:

我正在将 pandas 数据框导出为 html 表格,并且一直在尝试对最终表格中的标题列进行样式设置。这是我生成的示例数据框:

x = np.arange(0,100,5)
y = np.arange(0,20,1)
example_df = pd.DataFrame(x,y).head(10).reset_index()
example_df.columns = ['X Column', 'Y Column']

我已经能够使用以下代码编辑标题文本颜色:

example_df = example_df.to_html(index=False).replace('<th>','<th style = "color", "White">')

我还可以使用以下代码单独更改背景颜色:

example_df = example_df.to_html(index=False).replace('<th>','<th style = "background-color", "RoyalBlue">')

虽然我已经能够分别更改列标题文本颜色和背景颜色,但我无法同时更改两者。有没有更好的方法来实现这一点,以便我能够同时更改文本颜色和背景颜色,而不是在仅更改一个或另一个之间进行选择?谢谢!

【问题讨论】:

    标签: python html pandas pdf-to-html


    【解决方案1】:

    这会起作用 -

    > example_df.to_html(index=False).replace('<th>','<th style =
    > "background-color: royalblue; color: white">')
    

    【讨论】:

      猜你喜欢
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多