简体   繁体   中英

python xlutils/xlwt/xlrd/excel - can't modify copied worksheets

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM