Reputation: 1772
I'm strugging with xlutils and copying a worksheet. The code below successfully copies the first worksheet and adds 2 copies, however when I write to each worksheet it applies the changes to all worksheet copies and also the original.
So the example below, the value of row 3, column 0/1 on all 3 worksheets is "test2".
Does anyone know what I'm doing wrong here? I've also tried adding each clone, then trying a workbook.get_sheet(id).write(x,x,x) and the exact same thing happens.
workbook = xlutils.copy.copy(xlrd.open_workbook(xls_out, formatting_info=True))
tmp_workbook = copy(workbook)
copied_sheet = copy(tmp_workbook.get_sheet(0))
copied_sheet.set_name("tmpsheet")
copied_sheet.write(3, 0, 'test1')
copied_sheet.write(3, 1, 'test1')
workbook._Workbook__worksheets.append(copied_sheet)
copied_sheet = copy(tmp_workbook.get_sheet(0))
copied_sheet.set_name("tmpsheet2")
copied_sheet.write(3, 0, 'test2')
copied_sheet.write(3, 1, 'test2')
workbook._Workbook__worksheets.append(copied_sheet)
workbook.save(xls_out)
Upvotes: 2
Views: 669
Reputation: 11
Make use of deepcopy. Copy insert references to the objects found in the original and deepcopy constructs a new compound and then inserts copies into it of the objects found in the original.
Upvotes: 1