Skip to content

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

Closed
@HHammond

Description

@HHammond

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

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