python - 如何为 pandas dataframe 创建的 Excel 工作表中的表格添加边框?

标签 python excel pandas dataframe border

import pandas as pd
table =[[1,2,3,4],[11,12,13,14],["Pass","Fail","Pass","Fail"]]
df = pd.DataFrame(table)
df = df.transpose()
headers=["Current_Value","Previous_Value","Result",]
df.columns =headers

writer = pd.ExcelWriter("pandas.xlsx")

df.to_excel(writer, sheet_name='Sheet1')
writer.save()

此代码将创建一个表头以粗体显示。我想为 Excel 工作表中的表格添加边框。这可能吗?

最佳答案

您可以使用 StyleFrame 将边框/颜色添加到 Excel 中的数据框。

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

table =[[1,2,3,4],[11,12,13,14],["Pass","Fail","Pass","Fail"]]
df = pd.DataFrame(table)
df = df.transpose()
headers=["Current_Value","Previous_Value","Result",]
df.columns =headers

writer = StyleFrame.ExcelWriter("pandas.xlsx")

sf=StyleFrame(df)

sf.apply_column_style(cols_to_style=df.columns, styler_obj=Styler(bg_color=utils.colors.white, bold=True, font=utils.fonts.arial,font_size=8),style_header=True)

sf.apply_headers_style(styler_obj=Styler(bg_color=utils.colors.blue, bold=True, font_size=8, font_color=utils.colors.white,number_format=utils.number_formats.general, protection=False))

sf.to_excel(writer, sheet_name='Sheet1')
writer.save()

关于python - 如何为 pandas dataframe 创建的 Excel 工作表中的表格添加边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45493835/

相关文章:

php - 导出到 Excel 时出现奇怪的字符

python - 我必须从给定的 Dataframe : 中获取 "True"值多于 "False"值的组

python - 在 Odoo 网络表单中上传文件

python - 在 SQL select 查询中,删除第一个和最后一个 0 值,但不要删除中间的值

python - python 中对象的字典键项

excel - Excel 数据验证下拉列表中的空白值

python - 转置自动创建的列表中的数据

excel - 在文本框中使单词加粗

python - 使用 Pandas 将月度数据重新采样为年度数据,但从某个月份开始

python - 如何使用具有大量行的 DataFrame 使线图可读