Python ExcelWriter格式化“所有边界”

时间:2019-04-30 21:20:35

标签: python pandas pandas.excelwriter

我有这个enter image description here

我想要这个

enter image description here

我看了看文档,但是看不到一个能达到这个目的的函数。

1 个答案:

答案 0 :(得分:2)

您可以使用以下格式将格式添加到列中(必须使用样式来获得所需的厚度):

import pandas as pd
import numpy as np
import xlsxwriter

df = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), columns=['a', 'b', 'c'])
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
worksheet = writer.sheets['Sheet1']

border_fmt = workbook.add_format({'bottom':5, 'top':5, 'left':5, 'right':5})
worksheet.conditional_format(xlsxwriter.utility.xl_range(0, 0, len(df), len(df.columns)), {'type': 'no_errors', 'format': border_fmt})
writer.save()

这是格式示例的链接:https://xlsxwriter.readthedocs.io/format.html