@@ -1655,7 +1655,7 @@ class ExcelFormatter(object):
1655
1655
1656
1656
Parameters
1657
1657
----------
1658
- df : dataframe
1658
+ df : dataframe or Styler
1659
1659
na_rep: na representation
1660
1660
float_format : string, default None
1661
1661
Format string for floating point numbers
@@ -1675,13 +1675,22 @@ class ExcelFormatter(object):
1675
1675
inf_rep : string, default `'inf'`
1676
1676
representation for np.inf values (which aren't representable in Excel)
1677
1677
A `'-'` sign will be added in front of -inf.
1678
+ style_converter : callable, optional
1679
+ This translates Styler styles (CSS) into ExcelWriter styles.
1680
+ It should have signature css_list -> dict or None.
1681
+ This is only called for body cells.
1678
1682
"""
1679
1683
1680
1684
def __init__ (self , df , na_rep = '' , float_format = None , cols = None ,
1681
1685
header = True , index = True , index_label = None , merge_cells = False ,
1682
- inf_rep = 'inf' ):
1686
+ inf_rep = 'inf' , style_converter = None ):
1683
1687
self .rowcounter = 0
1684
1688
self .na_rep = na_rep
1689
+ if hasattr (df , 'render' ):
1690
+ self .styler = df
1691
+ df = df .data
1692
+ else :
1693
+ self .styler = None
1685
1694
self .df = df
1686
1695
if cols is not None :
1687
1696
self .df = df .loc [:, cols ]
@@ -1692,6 +1701,7 @@ def __init__(self, df, na_rep='', float_format=None, cols=None,
1692
1701
self .header = header
1693
1702
self .merge_cells = merge_cells
1694
1703
self .inf_rep = inf_rep
1704
+ self .style_converter = style_converter
1695
1705
1696
1706
def _format_value (self , val ):
1697
1707
if lib .checknull (val ):
@@ -1802,7 +1812,6 @@ def _format_regular_rows(self):
1802
1812
if has_aliases or self .header :
1803
1813
self .rowcounter += 1
1804
1814
1805
- coloffset = 0
1806
1815
# output index and index_label?
1807
1816
if self .index :
1808
1817
# chek aliases
@@ -1829,15 +1838,11 @@ def _format_regular_rows(self):
1829
1838
if isinstance (self .df .index , PeriodIndex ):
1830
1839
index_values = self .df .index .to_timestamp ()
1831
1840
1832
- coloffset = 1
1833
1841
for idx , idxval in enumerate (index_values ):
1834
1842
yield ExcelCell (self .rowcounter + idx , 0 , idxval , header_style )
1835
1843
1836
- # Write the body of the frame data series by series.
1837
- for colidx in range (len (self .columns )):
1838
- series = self .df .iloc [:, colidx ]
1839
- for i , val in enumerate (series ):
1840
- yield ExcelCell (self .rowcounter + i , colidx + coloffset , val )
1844
+ for cell in self ._generate_body (coloffset = 1 ):
1845
+ yield cell
1841
1846
1842
1847
def _format_hierarchical_rows (self ):
1843
1848
has_aliases = isinstance (self .header , (tuple , list , np .ndarray , Index ))
@@ -1902,11 +1907,26 @@ def _format_hierarchical_rows(self):
1902
1907
indexcolval , header_style )
1903
1908
gcolidx += 1
1904
1909
1910
+ for cell in self ._generate_body (coloffset = gcolidx ):
1911
+ yield cell
1912
+
1913
+ def _generate_body (self , coloffset ):
1914
+ if self .style_converter is None or self .styler is None :
1915
+ styles = None
1916
+ else :
1917
+ styles = self .styler ._compute ().ctx
1918
+ if not styles :
1919
+ styles = None
1920
+ xlstyle = None
1921
+
1905
1922
# Write the body of the frame data series by series.
1906
1923
for colidx in range (len (self .columns )):
1907
1924
series = self .df .iloc [:, colidx ]
1908
1925
for i , val in enumerate (series ):
1909
- yield ExcelCell (self .rowcounter + i , gcolidx + colidx , val )
1926
+ if styles is not None :
1927
+ xlstyle = self .style_converter (styles [i , colidx ])
1928
+ yield ExcelCell (self .rowcounter + i , colidx + coloffset , val ,
1929
+ xlstyle )
1910
1930
1911
1931
def get_formatted_cells (self ):
1912
1932
for cell in itertools .chain (self ._format_header (),
0 commit comments