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

ENH: support Styler in ExcelFormatter #15530

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions pandas/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ class ExcelFormatter(object):

Parameters
----------
df : dataframe
df : dataframe or Styler
na_rep: na representation
float_format : string, default None
Format string for floating point numbers
Expand All @@ -1675,13 +1675,22 @@ class ExcelFormatter(object):
inf_rep : string, default `'inf'`
representation for np.inf values (which aren't representable in Excel)
A `'-'` sign will be added in front of -inf.
style_converter : callable, optional
This translates Styler styles (CSS) into ExcelWriter styles.
It should have signature css_list -> dict or None.
This is only called for body cells.
"""

def __init__(self, df, na_rep='', float_format=None, cols=None,
header=True, index=True, index_label=None, merge_cells=False,
inf_rep='inf'):
inf_rep='inf', style_converter=None):
self.rowcounter = 0
self.na_rep = na_rep
if hasattr(df, 'render'):
self.styler = df
df = df.data
else:
self.styler = None
self.df = df
if cols is not None:
self.df = df.loc[:, cols]
Expand All @@ -1692,6 +1701,7 @@ def __init__(self, df, na_rep='', float_format=None, cols=None,
self.header = header
self.merge_cells = merge_cells
self.inf_rep = inf_rep
self.style_converter = style_converter

def _format_value(self, val):
if lib.checknull(val):
Expand Down Expand Up @@ -1802,7 +1812,6 @@ def _format_regular_rows(self):
if has_aliases or self.header:
self.rowcounter += 1

coloffset = 0
# output index and index_label?
if self.index:
# chek aliases
Expand All @@ -1829,15 +1838,11 @@ def _format_regular_rows(self):
if isinstance(self.df.index, PeriodIndex):
index_values = self.df.index.to_timestamp()

coloffset = 1
for idx, idxval in enumerate(index_values):
yield ExcelCell(self.rowcounter + idx, 0, idxval, header_style)

# Write the body of the frame data series by series.
for colidx in range(len(self.columns)):
series = self.df.iloc[:, colidx]
for i, val in enumerate(series):
yield ExcelCell(self.rowcounter + i, colidx + coloffset, val)
for cell in self._generate_body(coloffset=1):
yield cell

def _format_hierarchical_rows(self):
has_aliases = isinstance(self.header, (tuple, list, np.ndarray, Index))
Expand Down Expand Up @@ -1902,11 +1907,26 @@ def _format_hierarchical_rows(self):
indexcolval, header_style)
gcolidx += 1

for cell in self._generate_body(coloffset=gcolidx):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if uninterested in merging the whole patch, could you consider merging just this refactoring, so that it's easier to extend ExcelFormatter?

yield cell

def _generate_body(self, coloffset):
if self.style_converter is None or self.styler is None:
styles = None
else:
styles = self.styler._compute().ctx
if not styles:
styles = None
xlstyle = None

# Write the body of the frame data series by series.
for colidx in range(len(self.columns)):
series = self.df.iloc[:, colidx]
for i, val in enumerate(series):
yield ExcelCell(self.rowcounter + i, gcolidx + colidx, val)
if styles is not None:
xlstyle = self.style_converter(styles[i, colidx])
yield ExcelCell(self.rowcounter + i, colidx + coloffset, val,
xlstyle)

def get_formatted_cells(self):
for cell in itertools.chain(self._format_header(),
Expand Down