xlwt set style making error: More than 4094 XFs (s

2019-02-09 03:45发布

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

3条回答
够拽才男人
2楼-- · 2019-02-09 04:14

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楼-- · 2019-02-09 04:21

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!

查看更多
\"骚年 ilove
4楼-- · 2019-02-09 04:26

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.

查看更多
登录 后发表回答