Skip to content

Styling of column and row headers not reflected when saving to excel. #25185

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
saihari opened this issue Feb 6, 2019 · 3 comments
Closed

Styling of column and row headers not reflected when saving to excel. #25185

saihari opened this issue Feb 6, 2019 · 3 comments
Labels
Enhancement IO Excel read_excel, to_excel Styler conditional formatting using DataFrame.style

Comments

@saihari
Copy link

saihari commented Feb 6, 2019

Following is the code I used to style the headers of both rows and columns and then saving the stylized data frame to an excel file.

df = pd.DataFrame([1000000.0, 1000000.0], index=pd.bdate_range('2017-08-01', '2017-08-02'))

df.index = df.index.strftime("%Y-%m-%d")
styler = df.style
styler.set_table_styles(
# select the table header with th and set it right align
    [dict(selector="th", props=[("text-align", "right"),('color','green')])]   
)
styler.to_excel('styled.xlsx', engine='openpyxl')

Problem description

I am trying to stylize the headers of a data frame and then save that data frame to an excel file, but I am unable to see the stylized effects inside the excel file.

Output of Jupyter Notebook

image

Output in excel file

image

Expected Output

image

Pandas Version: '0.24.1'

@gfyoung gfyoung added IO Excel read_excel, to_excel Enhancement labels Feb 7, 2019
@gfyoung
Copy link
Member

gfyoung commented Feb 7, 2019

I was unaware that our Excel IO functionality could even support that level of formatting (or styling) for DataFrame. That being said, it would be nice if we could...

@WillAyd
Copy link
Member

WillAyd commented Feb 8, 2019

This is somewhat related to #22773

So right now there's not a great API to do this. This is controlled internally by the header_style property of the ExcelFormatter class:

def header_style(self):

That's not part of the exposed API, nor is it an exposed attribute of the Styler object as that instantiates it locally:

formatter = ExcelFormatter(self, na_rep=na_rep, cols=columns,

So one option is to figure out a way to logically expose that, though I'm not sure if that would really jive with the HTML elements that you are styling above.

In any case investigation and PRs would certainly be welcome

@WillAyd WillAyd added the Code Style Code style, linting, code_checks label Jun 10, 2019
@WillAyd WillAyd added this to the Contributions Welcome milestone Jun 10, 2019
@jbrockmendel jbrockmendel removed the Code Style Code style, linting, code_checks label Oct 16, 2019
@mroeschke mroeschke added the Styler conditional formatting using DataFrame.style label Jun 26, 2021
@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

6 participants