Skip to content

Styler class doesn't apply formatting from Styler.format when writing to Excel #21221

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
HHammond opened this issue May 27, 2018 · 5 comments
Closed
Labels
Enhancement IO Excel read_excel, to_excel Styler conditional formatting using DataFrame.style

Comments

@HHammond
Copy link
Contributor

HHammond commented May 27, 2018

Code Sample, a copy-pastable example if possible

import pandas as pd
df = pd.DataFrame({'a': [.1, .2], 'b': [.2, .3]})
df.style.background_gradient().format("{:.2%}").to_excel('test.xlsx')

or to prevent actually writing to disk:

buf = StringIO()
df = pd.DataFrame({'a': [.1, .2], 'b': [.2, .3]})

df.style.background_gradient().format("{:.2%}".format).to_excel(buf)

buf.seek(0)
pd.read_excel(buf)

When reading the file the gradient will be applied correctly but the percentage formatting will not be.

Problem description

When the Styler.to_excel is called it doesn't write styles added using the Styler.format method. The desired behaviour should be that all styling and formatting associated with the Styler object should be written to the excel file just as it gets rendered in HTML.

The excel writer being used is openpyxl: 2.5.3

Expected Output

Excel document with formatting operations included.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.23.0
pytest: None
pip: 9.0.3
setuptools: 39.0.1
Cython: None
numpy: 1.14.3
scipy: None
pyarrow: None
xarray: None
IPython: 5.7.0
sphinx: 1.7.4
patsy: None
dateutil: 2.7.3
pytz: 2018.4
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: 2.5.3
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@HHammond HHammond changed the title Styler class doesn't apply formatting when writing to Excel Styler class doesn't apply formatting from Styler.format` when writing to Excel May 27, 2018
@HHammond HHammond changed the title Styler class doesn't apply formatting from Styler.format` when writing to Excel Styler class doesn't apply formatting from Styler.format when writing to Excel May 27, 2018
@jbrockmendel jbrockmendel added the IO Excel read_excel, to_excel label Jul 30, 2018
@HHammond
Copy link
Contributor Author

Is this issue still alive?

@TomAugspurger
Copy link
Contributor

Still open.

@TomAugspurger TomAugspurger added this to the Contributions Welcome milestone Oct 12, 2018
@marknsikora
Copy link
Contributor

This can be worked around using #22015. You can use the number-format pseudo css property to apply display styling.

@jbrockmendel jbrockmendel added the Styler conditional formatting using DataFrame.style label Dec 11, 2019
@mroeschke mroeschke added the Bug label Apr 4, 2020
@attack68
Copy link
Contributor

The specific format strings needed for excel and the comparison to pythons much more generalist formatting language are not compatible.

The number-format property is a good way to deal with this , with the user specifying the excel format directly.

Still not perfect though. Outstanding issues like

  • [] excel format 'hh:mm:ss' or '£#,##0;-£#,##0' cannot be parsed due to : ; conflicting with css language.
  • [] datetimes are passed with a format 'yyyy-mm-dd hh:mm' by default and it seems this cannot be overwritted.

df = pd.DataFrame({
    'ints': np.arange(6, dtype=np.int64),
    'floats': np.arange(6, dtype=np.float64)*-100000,
    'bools': [True] * 6,
    'time': pd.date_range(start='2020-01-01', periods=6),
    'strings': ['a', 'b', 'c', 'd', 'e', 'juice'],
})
s = df.style

excel = [
    'General',
    '0',
    '0.00',
    '0%',
    'dd-mmm-yy',
    'dd/mm/yy',
]

s.apply(lambda s: [f'number-format:{x};' for x in excel]).to_excel('FORMAT_TEST.xlsx')

@attack68
Copy link
Contributor

closing this 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

6 participants