Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
r-ook opened this issue May 28, 2020 · 3 comments
Closed
3 tasks done

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

r-ook opened this issue May 28, 2020 · 3 comments
Labels
Enhancement IO Excel read_excel, to_excel Styler conditional formatting using DataFrame.style

Comments

@r-ook
Copy link

r-ook commented May 28, 2020

  • 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

@r-ook r-ook added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 28, 2020
@phofl
Copy link
Member

phofl commented May 31, 2020

Hey,

thanks for your report. I checked that this still exists on master.

@jbrockmendel jbrockmendel added Styler conditional formatting using DataFrame.style and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 6, 2020
@attack68
Copy link
Contributor

I think the excel styles are generated only from the ctx object on Styler which is a dict keyed by data cell rows, e.g. (2,3).

Only the apply and applymap methods update the ctx object.

This has the following implications:

  • Header elements (<th>) (index/columns) cannot be formatted for excel since they can only be styled using table_styles.
  • Styling with named css-classes, in combination with table_styles or external CSS also won't work.

It might be a considerable undertaking to incorporate table_styles and/or merge with class specifications.

An interim solution might be to simply detect obvious table_styles selectors like th, .row-heading or .col-heading

For now, I expect the best thing to do is enhance the documentation on this.

@attack68
Copy link
Contributor

closed as recorded in master tracker #42276

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO Excel read_excel, to_excel Styler conditional formatting using DataFrame.style
Projects
None yet
Development

No branches or pull requests

4 participants