更多pandas style用法请参考:https://pandas.liuzaoqi.com/doc/chapter8/style.html
示例程序
import numpy as np
import pandas as pd
# 示例数据
dataframe = pd.DataFrame({
"date": pd.date_range("2024-01-01", "2024-02-01"),
"num": np.random.random(size=32),
})
# 样式数据
style_df = dataframe.style.applymap(lambda x: 'color:red', subset=["date"]) \
.applymap(lambda x: 'color:green', subset=["num"])
# 保存到文件中
writer = pd.ExcelWriter("样式文件.xlsx")
style_df.to_excel(writer, index=False)
writer.close()
最后的结果如下: