pythonpandasstyleframe

saving style of the header of StyleFrame.object to_excel


When I save a StyleFrame object using sf.to_excel(), the header's style does not get saved to the xls file. Xls file's header always comes up in Arial 12 which appears to me as some default. Right before calling sf.to_excel() my sf.columns[0].container.style.font has the font I want (Calibri), but the xls file shows the header in Arial 12. Whereas data rows in xls are fine as they show the style I kept on sf's data rows.

Is there a way to control the header's style while using sf.to_excel()?


Solution

  • The correct way to style the headers is to use the apply_headers_style method:

    from StyleFrame import StyleFrame, Styler
    
    sf = StyleFrame({'a': [1, 2], 'b': [3, 4]})
    sf.apply_headers_style(Styler(font='Calibri'))
    sf.to_excel().save()
    

    This will style all the headers. A workaround if you want to style only the first column's header:

    from StyleFrame import StyleFrame
    
    sf = StyleFrame({'a': [1, 2], 'b': [3, 4]})
    sf.columns[0].style.font = 'Calibri'
    sf._has_custom_headers_style = True
    sf.to_excel().save()
    

    A future version will allow to pass cols_to_style argument to apply_headers_style.