Python-更改数据框的标题颜色并将其保存到Excel文件 [英] Python - change header color of dataframe and save it to excel file

查看:53
本文介绍了Python-更改数据框的标题颜色并将其保存到Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框 df ,我想在其中更改标题背景颜色,应用边框并将其以.xlsx扩展名格式保存.

I have a dataframe df where i want to change the header background color, apply borders and save it excel file in .xlsx extension.

我尝试过样式框架,openpyxl中的某些功能并尝试编写udf,但是似乎没有任何效果.

I have tried styleframe, some functionalities in openpyxl and tried to write udf s, But nothing seemed to work.

推荐答案

以下是使用 StyleFrame 您提到的包裹.

Here is the solution using StyleFrame package that you mentioned.

import pandas as pd
from StyleFrame import StyleFrame, Styler, utils

df = pd.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]})
sf = StyleFrame(df)

sf.apply_headers_style(styler_obj=Styler(bold=True,
                                         bg_color=utils.colors.green,
                                         border_type=utils.borders.medium))

sf.to_excel('output.xlsx').save()

我建议您确保已安装最新版本的StyleFrame.

I would recommend you to make sure that you have the lastest version of StyleFrame installed.

pip install -U styleframe

这篇关于Python-更改数据框的标题颜色并将其保存到Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆