xlsxwriter not applying format to header row of dataframe - Python Pandas

后端 未结 2 816
后悔当初
后悔当初 2020-12-17 04:02

I am trying to take a dataframe and create a spreadsheet from that dataframe using the xlsxwriter

I am trying to do some formatting to the header row, but the only f

相关标签:
2条回答
  • 2020-12-17 04:13

    In case you have 0.22, you must do pd.io.formats.excel.header_style = None. Check this git page out.

    0 讨论(0)
  • 2020-12-17 04:24

    You are trying to change the formatting of the header so you should first reset the default header settings

    pd.core.format.header_style = None
    

    Then apply the formatting as required

    format = workbook.add_format()
    format.set_align('center')
    format.set_align('vcenter')
    
    worksheet.set_column('A:C',5, format)
    

    here is complete working code

    d=pd.DataFrame({'a':['a','a','b','b'],
                   'b':['a','b','c','d'],
                   'c':[1,2,3,4]})
    d=d.groupby(['a','b']).sum()
    
    pd.core.format.header_style = None
    
    writer = pd.ExcelWriter('pandas_out.xlsx', engine='xlsxwriter')
    workbook  = writer.book
    d.to_excel(writer, sheet_name='Sheet1')
    
    worksheet = writer.sheets['Sheet1']
    
    format = workbook.add_format()
    format.set_align('center')
    format.set_align('vcenter')
    
    worksheet.set_column('A:C',5, format)
    writer.save()
    
    0 讨论(0)
提交回复
热议问题