2

I need to modify some one specific worksheet in an existing xls file and then save it again , using python.

The commonly suggested modules , openpyxl and xlsxwriter , do not support the older xls format.

I tried using the combination of xlrd, xlutils and xlwt. However I'm encountering what seems like a bug in the xlutils copy function. Here is roughly what I'm doing :

rb = xlrd.open_workbook("original.xls",formatting_info = True,on_demand=True)
# .. then some code to read in some data i need ...
wb = xlutils.copy.copy(rb) #use copy to get a xlwt workbook
sheet =  wb.get_sheet(sheet_number)
#use sheet.write() to add values i need to add
wb.save("modified.xls")

But due to some weird behaviour of the copy function, cell colors are getting changed across the entire new workbook ( green cells in the original became light blue , purple became dark blue ). Further, cell comments in the original are not present in the modified xls. So :

1) Is this a known bug in xlutils.copy which causes cell colors to change ? If so, is there any workaround for this ?

2) How do I get cell comments from the original to the new xls ?

  • possible duplicate http://stackoverflow.com/questions/3723793/preserving-styles-using-pythons-xlrd-xlwt-and-xlutils-copy – xiº Mar 27 '16 at 15:32
  • In http://stackoverflow.com/questions/3723793/preserving-styles-using-pythons-xlrd-xlwt-and-xlutils-copy what the op wanted to know was whether you can modify an existing cell without losing/overwriting its style info. My case is simpler. I'm not trying to edit existing cells, just append new rows to a specific sheet in the workbook . Yet , for some reason, all the worksheets in the new xls (even ones i havnt touched at all ) have their color info altered when i inspect it with excel after calling "save" . – welcomedungeon Mar 27 '16 at 16:35
  • Just a shot in the dark: Does it make any difference if you omit the `on_demand` parameter? – John Y Mar 30 '16 at 21:48

0 Answers0