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

2019-02-09 03:56发布

问题:

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.