Skip to content

BUG: Styler.set_table_styles does not apply to Styler.to_excel #34438

Closed
@r-ook

Description

@r-ook
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


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:

<style  type="text/css" >
    #T_4987f224_a119_11ea_b2cc_48452026764f th {
          background-color: orange;
    }    #T_4987f224_a119_11ea_b2cc_48452026764frow0_col0 {
            color:  red;
            color:  red;
            color:  red;
            color:  red;
            color:  red;
        }    #T_4987f224_a119_11ea_b2cc_48452026764frow0_col1 {
            color:  blue;
            color:  blue;
            color:  blue;
            color:  blue;
            color:  blue;
        } <!-- ...continues ... -->
with open(r'pd_css_test.html', 'w') as f:
	f.write(style.render())

Expected Output

Unable to show excel/colors here, but the columns/index should have colour styling applied as expected from set_table_styles.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.4.final.0
python-bits : 32
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 61 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_Canada.1252

pandas : 1.0.3
numpy : 1.18.4
pytz : 2020.1
dateutil : 2.8.1
pip : 19.2.3
setuptools : 40.8.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : None
numexpr : None
odfpy : None
openpyxl : 3.0.0
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : 1.2.8
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementIO Excelread_excel, to_excelStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions