Wrong background color after xlutils.copy

100 views
Skip to first unread message

Alexander Zverev

unread,
Jun 3, 2022, 11:02:19 AM6/3/22
to python-excel
Hello! I'm trying to to process a file from our customers, but after modifying the file colors of all cells are wrong.

I simplified code, but that didn't work. Even Open -> Copy -> Save works same

from xlrd import open_workbook
from xlutils.copy import copy
import xlwt
original = open_workbook(filename="cleared.xls", formatting_info=True)
filled = copy(original)
filled.save("cleared_copied.xls")

What could be the reason of it?
Screenshot_1.jpg
cleared.xls
cleared_copied.xls

Alexander Zverev

unread,
Jun 7, 2022, 5:17:48 AM6/7/22
to python-excel
If someone interested, I solved this by redefining method _Workbook__palette_rec of object with editable table. By default, it does not return standard styling.

from xlrd import open_workbook
from xlutils.copy import copy
import xlwt
from types import MethodType

def _Workbook__palette_rec_new(self):
     excel_modified_default_palette_b8 = (
     0x00000000,
     0xFFFFFF00,
     0xFF000000,
     0x00FF0000,
     0x0000FF00,
     0xFFFF0000,
     0xFF00FF00,
     0x00FFFF00,
     0x80000000,
     0x00800000,
     0x00008000,
     0x80800000,
     0x80008000,
     0x00808000,
     0xC0C0C000,
     0x80808000,
     0xF5F2DD00, # Main color
     0x99336600,
     0x59430400, # Font color
     0xCCFFFF00,
     0x66006600,
     0x90EE9000, # Fillable cells
     0x0066CC00,
     0xCCCCFF00,
     0x00008000,
     0xFF00FF00,
     0xFFFF0000,
     0x00FFFF00,
     0x80008000,
     0x80000000,
     0x00808000,
     0x0000FF00,
     0x00CCFF00,
     0xCCFFFF00,
     0xCCFFCC00,
     0xFFFF9900,
     0x99CCFF00,
     0xFF99CC00,
     0xCC99FF00,
     0xFFCC9900,
     0x3366FF00,
     0x33CCCC00,
     0x99CC0000,
     0xFFCC0000,
     0xFF990000,
     0xFF660000,
     0x66669900,
     0x96969600,
     0x00336600,
     0x33996600,
     0x00330000,
     0x33330000,
     0x99330000,
     0x99336600,
     0x33339900,
     0x33333300)
     return xlwt.BIFFRecords.PaletteRecord(excel_modified_default_palette_b8).get()


original = open_workbook(filename="cleared.xls", formatting_info=True)
filled = copy(original)
filled._Workbook__palette_rec = MethodType(_Workbook__palette_rec_new, filled)
filled.save("cleared_filled.xls")
    
пятница, 3 июня 2022 г. в 18:02:19 UTC+3, Alexander Zverev:
Reply all
Reply to author
Forward
0 new messages