xlsxwriter将excel文档中第一列的字体样式更改为粗体。如何禁用此选项?

时间:2019-05-22 08:15:27

标签: python pandas

这两个openpyxl中的一个,xlsxwriter在将csv文件导出到excel时会自动将字体样式更改为粗体。它仅发生在第一列。您知道为什么以及如何克服这种行为吗?

import pandas as pd
import openpyxl
import xlsxwriter
from pandas import DataFrame
import time
from glob import iglob

 data = pd.read_csv(next(iglob('*.csv')))
 data = data.sort_values(by=['A'], ascending=False)
 data.to_excel('out.xlsx',engine='xlsxwriter', index=False)

1 个答案:

答案 0 :(得分:0)

您可以将自定义pd.ExcelWriter实例传递给pd.DataFrame.to_excel(),并在标题列上显式禁用任何样式:

import pandas as pd

df = pd.DataFrame(data={'col_a': [1,2,3,4], 'col_b': [5,6,7,8]})

with pd.ExcelWriter('out.xlsx') as writer:
    df.to_excel(writer, index=False)

    workbook  = writer.book
    worksheet = writer.sheets['Sheet1']

    header_format = workbook.add_format({
        'bold': False,
        'border': False})

    for col_num, value in enumerate(df.columns.values):
        worksheet.write(0, col_num, value, header_format)

编辑[pandas 0.24不再支持] :或者,您可以reset all pandas default header styling。之后,只需照常致电df.to_excel()

import pandas.io.formats.excel
pandas.io.formats.excel.header_style = None