Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

excel

xlwt

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

like image 992
happy Sun Avatar asked Jun 16 '13 05:06

happy Sun


3 Answers

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)
like image 190
fanny Avatar answered Nov 15 '22 17:11

fanny


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!

like image 23
happy Sun Avatar answered Nov 15 '22 18:11

happy Sun


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.

like image 21
Michał Hernas Avatar answered Nov 15 '22 17:11

Michał Hernas