xlwt set style making error: More than 4094 XFs (styles)

佐手、 提交于 2019-11-29 18:25:35

问题


I use Xlwt for writing an excel file. it's cells has some style (color, alignment ,borders , ... )

when i use XFStyle and set borders and other attr of style, in some cases it make error: More than 4094 XFs (styles)

why? what should i do with this error?

thanks


回答1:


I read and trace functions and methods that calls during execution.

i find solution:

wb = xlwt.Workbook(style_compression=2)

use : style_compression=2

its work!




回答2:


Upon encountering the same problem in the LOOP I have extracted the description of format from the loop and it continued smoothly:

this did not work:

for ... :
    ws. row(row_index).write(col_index, value, easyxf('pattern: pattern solid, fore_colour yellow; align: wrap 1'))

but this does:

ostyle = easyxf('pattern: pattern solid, fore_colour yellow; align: wrap 1')

for .... :
    ws.row(row_index).write(col_index, value,ostyle)



回答3:


So, for future generations, whoever you are searching for answer, you do something wrong in your code.

Basically, what happens with your code is that you generated over 4094 different styles instances (Important, not different styles, it is enough if you create new instances of style).

In our case we had something like:

for i, row in enumerate(rows):
    workbook.write(i, 0, row, currency_formatter(row))

Where currency formatter was created new style for each row.

What we had to do, was to cache style per each currency if style was the same.

So, correct fix is not to create so many styles!

Cheers, Mike.



来源:https://stackoverflow.com/questions/17130516/xlwt-set-style-making-error-more-than-4094-xfs-styles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!