Issue
I have a data frame in which I am making updations like writing and adding the background colour to a specific cell. It looks fine in dataframe
. But when I convert dataframe
to excel, found the below error.
df.to_excel("test_sheet.xls")
is causing the error.
Traceback (most recent call last):
File "scoring_model.py", line 441, in <module>
pf = ScoringModel()
File "scoring_model.py", line 12, in __init__
self.start_scoring()
File "scoring_model.py", line 185, in start_scoring
df.to_excel("test_sheet.xls")
File "D:\Projects\Scoring Model\venv\lib\site-packages\pandas\io\formats\style.py", line 187, in to_excel
engine=engine)
File "D:\Projects\Scoring Model\venv\lib\site-packages\pandas\io\formats\excel.py", line 662, in write
freeze_panes=freeze_panes)
File "D:\Projects\Scoring Model\venv\lib\site-packages\pandas\io\excel.py", line 1708, in write_cells
style = self._convert_to_style(cell.style, fmt)
File "D:\Projects\Scoring Model\venv\lib\site-packages\pandas\io\excel.py", line 1771, in _convert_to_style
style = xlwt.easyxf(xlwt_stylestr, field_sep=',', line_sep=';')
File "D:\Projects\Scoring Model\venv\lib\site-packages\xlwt\Style.py", line 733, in easyxf
field_sep=field_sep, line_sep=line_sep, intro_sep=intro_sep, esc_char=esc_char, debug=debug)
File "D:\Projects\Scoring Model\venv\lib\site-packages\xlwt\Style.py", line 638, in _parse_strg_to_obj
raise EasyXFCallerError('section %r is unknown' % section)
xlwt.Style.EasyXFCallerError: section 'fill' is unknown
using python3.5
and xlwt 1.3
latest.
Does anyone found the same error?
Thanks
Solution
Can you paste in the xlwt_stylestr
?
There could be a typo in there [missing a space character?], as I checked the origin of the exception - https://github.com/python-excel/xlwt/blob/master/xlwt/Style.py#L639
Based on the comments, could you try:
xlwt_stylestr = 'pattern: pattern solid, fore_colour red;'
And then pass it to the write
callable:
https://xlwt.readthedocs.io/en/latest/api.html#xlwt.Worksheet.Worksheet.write
styled.to_excel('styled.xlsx')
Can you try this:
def bg_red(val):
# this will receive each value in the excel
# so if you want to add conditional logic, you can
# add here
return 'background-color: red'
style_obj = df.style.applymap(bg_red)
style_obj.to_excel('styled_doc.xlsx')
Refer this for more: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html
Answered By - Nidhin Bose J.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.