【问题标题】:xlwt.Style.EasyXFCallerError: section 'fill' is unknownxlwt.Style.EasyXFCallerError:“填充”部分未知
【发布时间】:2019-12-18 18:17:27
【问题描述】:

我有一个数据框,我正在其中进行更新,例如将背景颜色写入特定单元格并将其添加到特定单元格。在dataframe 中看起来不错。但是当我将dataframe转换为excel时,发现如下错误。

df.to_excel("test_sheet.xls") 导致错误。

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

使用python3.5xlwt 1.3 最新。 有没有人发现同样的错误?

谢谢

【问题讨论】:

    标签: python excel python-3.x pandas xlwt


    【解决方案1】:

    虽然听起来很蹩脚,但我遇到了同样的困难,当我改变时就停止了

    df.to_excel("test_sheet.xls")

    df.to_excel("test_sheet.xlsx")

    FWIW 我也在使用“xlsxwriter”

    【讨论】:

    • 请在您的答案中突出显示更改。你解决了我的问题,即使我一开始没有注意到 xlsx 中的额外 X
    【解决方案2】:

    你能粘贴xlwt_stylestr吗?

    那里可能有错字[缺少空格字符?],因为我检查了异常的来源 - https://github.com/python-excel/xlwt/blob/master/xlwt/Style.py#L639

    根据cmets,你能试试吗:

    xlwt_stylestr = 'pattern: pattern solid, fore_colour red;'
    

    然后将其传递给write 可调用对象: https://xlwt.readthedocs.io/en/latest/api.html#xlwt.Worksheet.Worksheet.write styled.to_excel('styled.xlsx')

    你能试试这个吗:

    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')
    

    更多信息请参考: https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html

    【讨论】:

    • 这是我添加的样式字符串'background-color: Red',它在数据框中工作。
    • 用上面显示的方法试过了,但是不行,在数据框中也失败了。
    • 它没有显示任何错误,但没有显示颜色。
    • 您在写入数据时是否添加了style 对象[xlwt.easyxf(...)]?
    • 没有。我正在像这样使用df = df.style.apply(['pattern: pattern solid, fore_colour red;'])
    猜你喜欢
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 2021-12-16
    • 2019-08-22
    • 2018-12-15
    • 1970-01-01
    相关资源
    最近更新 更多