简体   繁体   English

python xlutils / xlwt / xlrd / excel-无法修改复制的工作表

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

I'm strugging with xlutils and copying a worksheet. 我正在努力与xlutils并复制工作表。 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. 下面的代码成功复制了第一个工作表并添加了2个副本,但是,当我写每个工作表时,它将所做的更改应用于所有工作表副本以及原始副本。

So the example below, the value of row 3, column 0/1 on all 3 worksheets is "test2". 因此,下面的示例在所有3个工作表上的第3行第0/1列的值为“ 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.get_sheet(id).write(x,x,x),然后发生完全相同的事情。

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. 利用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. 复制插入对原始对象中发现的对象的引用,而Deepcopy构造一个新的化合物,然后将原始对象中发现的对象的副本插入其中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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