Skip to content
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

Styling in DataFrame.to_excel #1663

Closed
wesm opened this issue Jul 23, 2012 · 14 comments
Closed

Styling in DataFrame.to_excel #1663

wesm opened this issue Jul 23, 2012 · 14 comments
Labels
Enhancement IO Data IO issues that don't fit into a more specific label IO Excel read_excel, to_excel

Comments

@wesm
Copy link
Member

wesm commented Jul 23, 2012

from mailing list

Hello everyone,

I wonder if it's possible to apply style to the outputed excel file or should I have to code a similar function with my style ?

Maybe by overloadind ?
@gerigk
Copy link

gerigk commented Jul 24, 2012

openpyxl supports styles but afaik you can't use them with the optimized writer than pandas uses by default. So instead of using sheet.append_row(row) one has to access each cell independently.
You can set the column width by column but attributes like colour or number format have to be set by cell and this slows everything down dramatically.
It's probably not too hard to add faster "by column" styles to openpyxl but unfortunately the project is very inactive (I added cell comments in May and never got any feedback for my pull request).

@aflaxman
Copy link
Contributor

I have been looking into this as well recently, and found this blog post and related gist on the matter.

@gerigk can you add a link to your PR? I can't find it.

@wesm interested in a PR on this? What does it need to do to be accepted?

@jreback
Copy link
Contributor

jreback commented Jun 26, 2014

@aflaxman see this as well: #7565

always interested in a PR!

pls write tests / code / update doc-strings

see here: https://github.com/pydata/pandas/wiki

@aflaxman
Copy link
Contributor

Ha ha, I just looked at the day and month, not the year. Does #7565 expose something analogous to the style parameter in https://gist.github.com/dmvianna/4602492#file-xlpandas2-py-L50 ? I couldn't find it with a quick search.

@jreback
Copy link
Contributor

jreback commented Jun 26, 2014

excel has multiple engines (xlsxwriter, xlwt, openpyxl), so is different for each.

@jreback
Copy link
Contributor

jreback commented Sep 21, 2014

closed by using this PR: #7565

@jreback jreback closed this as completed Sep 21, 2014
@jtratner
Copy link
Contributor

@jreback #7565 didn't really cover this, still need to decide how we'd actually want to expose styling, I'm thinking per-column styles could get us 90% of the way there. @aflaxman - if you have ideas for how an API ought to work, please feel free to comment here or in a new issue.

@jtratner jtratner reopened this Sep 21, 2014
@jreback
Copy link
Contributor

jreback commented Sep 21, 2014

@jtratner sure

@aflaxman
Copy link
Contributor

For my purposes, it would get 90% of the way to have a few defaults for highlighting the first row and/or the first column, and bonus points for zebra-striping a la excel. But the case that inspired me to comment on this ticket was a series of tables where there were one or two cells that I need to highlight, and for that I think some sort of table-painter interface would be best. Pandas already has plenty of functionality for this, so maybe just allowing an optional style dict in the .to_excel functions, e.g.

df.to_excel(path, style=style_df)

where style_df.shape = df.shape, and non-empty entries of style_df are style dicts a la the examples here #7565 (comment)

I'll try it out and report back here.

@aflaxman
Copy link
Contributor

Here is a notebook with a start in the direction I am thinking. It makes this:
image

Comments welcome!

@jreback
Copy link
Contributor

jreback commented Oct 25, 2014

cc @neirbowj

this is using the new openpyxl2 stuff?

@neirbowj
Copy link
Contributor

Yup, it sure looks that way. 😄

@aflaxman
Copy link
Contributor

yes, speaking of which, is the style dictionary documented any more than in the #7565 comments now?

@neirbowj
Copy link
Contributor

@aflaxman: No, it is not. However, the short, short version of the docs would go something like this: any keyword or literal value that the native openpyxl v2 style interface accepts, the pandas style dict should accept as well. Symbolic constants are the main exception that I can think of off hand (e.g. {'color':'RED'}).

So, for example, the default font, per the openpyxl docs, would look like this:

""""
Excerpt --
    font=Font(name='Calibri',
        size=11,
        bold=False,
        italic=False,
        vertAlign=None,
        underline='none',
        strike=False,
        color='FF000000')
""""
font = {
    'font': {
        'name': 'Calibri',
        'size': 11,
        'bold': False,
        'italic': False,
        'vertAlign': None,
        'underline': 'none',
        'strike': False,
        'color': 'FF000000',
    }
}

analyticalmonk pushed a commit to analyticalmonk/pandas that referenced this issue Apr 20, 2017
closes pandas-dev#1663

Author: Joel Nothman <joel.nothman@gmail.com>

Closes pandas-dev#15530 from jnothman/excel_style and squashes the following commits:

c7a51ca [Joel Nothman] Test currently fails on openpyxl1 due to version incompatibilities
836f39e [Joel Nothman] Revert changes to xlwt
de53808 [Joel Nothman] Remove debug code
a5d51f9 [Joel Nothman] Merge branch 'master' into excel_style
934df06 [Joel Nothman] Display df, not styled
6465913 [Joel Nothman] More pytest-like test_styler_to_excel; enhancements to xlwt
6168765 [Joel Nothman] Recommended changes to what's new
9669d7d [Joel Nothman] Require jinja in test with df.style
14035c5 [Joel Nothman] Merge branch 'master' into excel_style
3071bac [Joel Nothman] Complete tests
ceb9171 [Joel Nothman] reasons for xfails
e2cfa77 [Joel Nothman] Test Styler.to_excel
d5db0ac [Joel Nothman] Remove obsolete TODO
0256fc6 [Joel Nothman] Return after unhandled font size warning
60d6a3b [Joel Nothman] add doc/source/styled.xlsx to the gitignore
4e72993 [Joel Nothman] Fix what's new heading
d144fdf [Joel Nothman] Font name strings
61fdc69 [Joel Nothman] Complete testing basic CSS -> Excel conversions
6ff8a46 [Joel Nothman] Fix loose character; sorry
6d3ffc6 [Joel Nothman] Lint
79eae41 [Joel Nothman] Documentation tweaks
c4f59c6 [Joel Nothman] Doc tweaks
2c3d015 [Joel Nothman] Fix JSON syntax in IPynb
b1d774b [Joel Nothman] What's new heading
096f26c [Joel Nothman] Merge remote-tracking branch 'upstream/master' into excel_style
433be03 [Joel Nothman] Documentation
9a62699 [Joel Nothman] Fix tests and add TODOs to tests
7c54a69 [Joel Nothman] Fix test failures; avoid hair border which renders strangely
8e9a567 [Joel Nothman] Fixes from integration testing
c1fc232 [Joel Nothman] Remove debugging print statements
a43d6b7 [Joel Nothman] Cleaner imports
a1127f6 [Joel Nothman] Merge branch 'master' into excel_style
306eebe [Joel Nothman] Module-level docstring
350eab5 [Joel Nothman] remove spurious blank line
efce9b6 [Joel Nothman] More CSS to Excel testing; define ExcelFormatter.write
f17a0f4 [Joel Nothman] Some border style tests
1a8818f [Joel Nothman] Lint
9a5b791 [Joel Nothman] Fix testing ImportError
1984cab [Joel Nothman] Fix making get_level_lengths non-private
eb02cc1 [Joel Nothman] Fix testing ImportError
3b26087 [Joel Nothman] Make get_level_lengths non-private
f62f02d [Joel Nothman] File restructure
dc953d4 [Joel Nothman] Font size and border width
7db59c0 [Joel Nothman] Test inherited styles in converter
d103f61 [Joel Nothman] Refactoring and initial tests for CSS to Excel
176e51c [Joel Nothman] Fix NameError
c589c35 [Joel Nothman] Fix some lint errors (yes, the code needs testing)
cb5cf02 [Joel Nothman] Fix bug where inherited not being passed; avoid classmethods
0ce72f9 [Joel Nothman] Use inherited font size for em_pt
8780076 [Joel Nothman] Merge branch 'master' into excel_style
96680f9 [Joel Nothman] Largely complete CSSToExcelConverter and Styler.to_excel()
f1cde08 [Joel Nothman] FIX column offset incorrect in refactor
ada5101 [Joel Nothman] ENH: support Styler in ExcelFormatter
pcluo pushed a commit to pcluo/pandas that referenced this issue May 22, 2017
closes pandas-dev#1663

Author: Joel Nothman <joel.nothman@gmail.com>

Closes pandas-dev#15530 from jnothman/excel_style and squashes the following commits:

c7a51ca [Joel Nothman] Test currently fails on openpyxl1 due to version incompatibilities
836f39e [Joel Nothman] Revert changes to xlwt
de53808 [Joel Nothman] Remove debug code
a5d51f9 [Joel Nothman] Merge branch 'master' into excel_style
934df06 [Joel Nothman] Display df, not styled
6465913 [Joel Nothman] More pytest-like test_styler_to_excel; enhancements to xlwt
6168765 [Joel Nothman] Recommended changes to what's new
9669d7d [Joel Nothman] Require jinja in test with df.style
14035c5 [Joel Nothman] Merge branch 'master' into excel_style
3071bac [Joel Nothman] Complete tests
ceb9171 [Joel Nothman] reasons for xfails
e2cfa77 [Joel Nothman] Test Styler.to_excel
d5db0ac [Joel Nothman] Remove obsolete TODO
0256fc6 [Joel Nothman] Return after unhandled font size warning
60d6a3b [Joel Nothman] add doc/source/styled.xlsx to the gitignore
4e72993 [Joel Nothman] Fix what's new heading
d144fdf [Joel Nothman] Font name strings
61fdc69 [Joel Nothman] Complete testing basic CSS -> Excel conversions
6ff8a46 [Joel Nothman] Fix loose character; sorry
6d3ffc6 [Joel Nothman] Lint
79eae41 [Joel Nothman] Documentation tweaks
c4f59c6 [Joel Nothman] Doc tweaks
2c3d015 [Joel Nothman] Fix JSON syntax in IPynb
b1d774b [Joel Nothman] What's new heading
096f26c [Joel Nothman] Merge remote-tracking branch 'upstream/master' into excel_style
433be03 [Joel Nothman] Documentation
9a62699 [Joel Nothman] Fix tests and add TODOs to tests
7c54a69 [Joel Nothman] Fix test failures; avoid hair border which renders strangely
8e9a567 [Joel Nothman] Fixes from integration testing
c1fc232 [Joel Nothman] Remove debugging print statements
a43d6b7 [Joel Nothman] Cleaner imports
a1127f6 [Joel Nothman] Merge branch 'master' into excel_style
306eebe [Joel Nothman] Module-level docstring
350eab5 [Joel Nothman] remove spurious blank line
efce9b6 [Joel Nothman] More CSS to Excel testing; define ExcelFormatter.write
f17a0f4 [Joel Nothman] Some border style tests
1a8818f [Joel Nothman] Lint
9a5b791 [Joel Nothman] Fix testing ImportError
1984cab [Joel Nothman] Fix making get_level_lengths non-private
eb02cc1 [Joel Nothman] Fix testing ImportError
3b26087 [Joel Nothman] Make get_level_lengths non-private
f62f02d [Joel Nothman] File restructure
dc953d4 [Joel Nothman] Font size and border width
7db59c0 [Joel Nothman] Test inherited styles in converter
d103f61 [Joel Nothman] Refactoring and initial tests for CSS to Excel
176e51c [Joel Nothman] Fix NameError
c589c35 [Joel Nothman] Fix some lint errors (yes, the code needs testing)
cb5cf02 [Joel Nothman] Fix bug where inherited not being passed; avoid classmethods
0ce72f9 [Joel Nothman] Use inherited font size for em_pt
8780076 [Joel Nothman] Merge branch 'master' into excel_style
96680f9 [Joel Nothman] Largely complete CSSToExcelConverter and Styler.to_excel()
f1cde08 [Joel Nothman] FIX column offset incorrect in refactor
ada5101 [Joel Nothman] ENH: support Styler in ExcelFormatter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement IO Data IO issues that don't fit into a more specific label IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants