append()
方法对sheet添加数据时,数据可以是Python里的list、tuple、range、generator或则dict
wb = Workbook()
ws = wb.active
wb1 = load_workbook(filename)
ws1 = wb1['Sheet1']
for row in ws1.iter_rows():
ws.append([cell.value for cell in row])
这里如果插入的是单元格的值,则会报错,以下代码则会报错
for row in ws1.iter_rows():
list_to_append = [cell.value for cell in row]
for item in list_to_append:
ws.append(item)
所以这里整体一次性插入就可以,append()方法一次插入一行记录,再次调用会插入第二行记录,依次类推,所以利用iter_rows()方法和append()方法就可以实现对一个已有sheet表的copy。
另外也可以利用ws.cell(row=r, column=c)单元格操作进行一个一个单元格的copy操作
for r in range(1, ws1.max_row+1):
for c in range(1, ws1.max_column+1):
ws.cell(row=r, column=c) = ws1.(row=r, column=c)
sheet工作表操作完,需要保存的话,是对workbook进行保存
wb.save(filename)