0

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()?

Jorge
  • 83
  • 10

1 Answers1

2

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.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • 1
    The `sf._has_custom_headers_style = True` was just what I needed as my sf.columns already had embedded style because my sf was generated by `sf=StyleFrame.read_excel('my.xlsx', read_style=True)`. However, it would have been better if `read_excel()` had internally set the flag `sf._has_custom_headers_style=True` before returning the sf when it saw `read_style=True`. – Jorge Jul 14 '19 at 00:16
  • @Jorge It does: https://github.com/DeepSpace2/StyleFrame/blob/9721534560009c1ac61c28fcec47c026d6803aca/StyleFrame/style_frame.py#L221 – DeepSpace Jul 14 '19 at 07:42
  • You are correct. The flag is there. My sf must not have been saving header's style for some other reason (probably I was using `use_openpyxl_styles=False` or copying sf.columns). – Jorge Jul 14 '19 at 13:00