BUG: Styler.set_table_styles does not apply to Styler.to_excel · Issue #34438 · pandas-dev/pandas (original) (raw)


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd import numpy as np

Dummy dataframe

df = pd.DataFrame(np.random.rand(15).reshape(5,3), columns='x y z'.split())

Apply style with set_table_styles and applymap

style = df.style
.set_table_styles([{'selector': 'th', 'props': [('background-color', 'orange')]}])
.applymap(lambda x: 'color: red' if x >.5 else 'color: blue')

Test with different engines

for eng in ('xlsxwriter', 'openpyxl'): style.to_excel(f'tbl_style_tst_{eng}.xlsx', engine=eng)

Problem description

In both xlsx files output above, only the element styling (applymap) are observed. Any set_table_styles styling seems to be ignored regardless of engine. The html from style.render() confirms the styling is correct, and is observed in html output as well: