-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Styler.to_excel
does not export styles and formats correctly
#42276
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
Comments
At one point I tried changing the backend to use existing CSS parsing libraries so that issues like border specification would be more generic, but those I considered required substantial modification, and I ran out of time to pursue this... What's out there might have changed a lot in four years. |
Regarding border styles, the error in #30008 appears when a border color is defined but border style is not (or border style is none). The source of the error is in The root cause however is in In summary, I believe replacing each |
do you know with this solution how to format your styles, e.g.:
also with |
take |
Any updates on this issue? |
the updates have been provided where merged prs have been linked. any issues not currnetly being worked on do not have links or merged prs. |
Just for anyone who still want to style their table index, here is my solution: Note: This is a method which focuses on exporting your multi-index table as an excel with alternating colours for primal index, as long as it looks like what it is inside python, I dont care whether certain column in excel is really 'column' or 'index' inside python. First, do the routine:
Divide your dataframe df into two parts - coloured half and blank half. I am using light blue for this example.
Here is the tricky part. Reset your dataframe as single index but preserve the structure by filling NaN to extra primal index column, this way they will not show in the styled dataframe nor in the exported excel later.
Then apply the colouring with style and we are golden.
Then you will get one excel with table looking exactly the same as that in python. References: |
or just use |
didn't know we have that, tks! |
This issue is fired |
Background gradient is HTML and LaTeX only. This will not be ported to Excel. You are welcome to submit your own PR to fix pandas if the 3y timeline from software volunteers for a free library does not suit your requirements. Thanks for the message. |
Thanks for your info! |
If you use If you do this: import pandas as pd
df = pd.DataFrame([[1,2],[3,4]])
df.style.background_gradient().to_excel("my_wb.xlsx") you get this in Excel: If you feel you can document this better than currently, PRs are welcome and appreciated. |
@attack68 ,
As you can see, I'm the beginer of panas, I cannot impove it before understanding. |
|
@attack68 Why calling |
Becuase Jupyter Notebook and Excel are different software applications and they work in completely different ways. Please submit any further user questions to appropriate forums, e.g. stack overflow. |
This is a tracker / explainer for the various issues:
Essentially these issues record 3 things:
Set_table_styles
Styler.set_table_styles
is not exported to excel. This will not be changed (at least by me). To write xlsx, excel styling needs to be attached on a per-cell basis, whereas in HTML indirect references can be created in the CSS language that the browser will parse, for examplethead th
will apply to all header cells in the header section, andtbody th:nth-child(3n+0)
will apply to every third header cell in the body section starting with first. Without writing our own HTML to cell translator for the CSS language it is therefore impossible to map and account for all the complex CSS rules that can be used withinset_table_styles
. This is well documented.Todo:
Styler.apply(map)_index
made compatible withStyler.to_excel
#41995)Exporting formatting
The number of possible formatting constructs allowed by Python is greater than what excel offers. Excel also has specified structures that differ from Pythons structures. It is impossible to dynamically code these relationships in some ambiguous cases. Therefore this will not be implemented.
Todo:
number-format
which can be used to apply specific Excel based formatting. This should be much better documented with examples.Border styles bug
Borders in CSS can be specified in many different ways, and the parsing code to translate this into excel's border structure is broken
Todo
(BUG/ENH: Translate CSS border properties for
Styler.to_excel
#45312)Hiding and Concatening
The Styler uses the base implementation of DataFrame.to_excel. It does not do any preliminary filtering and/or alteration of the
ctx
object to format cells in the right place. It also doesnt react to hidden indexes and/or elements.Todo
The text was updated successfully, but these errors were encountered: