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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.