1. 使用开发工具为Visual Studio 2019,Python版本为3.7,扩展包版本如下图

2. 我这边有一个需求,在使用xlrd2读取一个Excel表格,然后使用xlutils.copy.copy功能编辑这个Excel,实现数据更新后重新保存这个Excel表格,表格式xls格式,问题是我在第一遍执行这个方法的时候,运行完全正常可以得到我想要的结果,但是执行第二遍时候就提示报错“list index out of range”

3. 我尝试修改了bk为bk1,xlrd2我也重新import一遍as xlrd22,然后使用xlrd22.open_workbook试了下,但是我检查参数的时候发现跟bk对象无关,在编程软件里我注意到bk已经完整的获取到表格数据了,所以我想问题出在copy方法上,但是完全不知道该如何修改来达到我的目的,没有任何头绪,,,excel大小是2445KB,总共12个sheet,求大佬们指点。
附上两段方法代码:
#结单量透视
def secondStepII():
bk = xlrd2.open_workbook("数据\\" + fileName + "二级.xls", formatting_info = True) # 打开文件
wt = xlutils.copy.copy(bk) # 复制
sh=bk.sheet_by_index(1) #使用wlrd读取sheet2
nrows=sh.nrows #获取行数
ncols=sh.ncols #获取列数
sheet = wt.get_sheet(7) # wlwt读取sheet8
# 向单元格写入内容
#sheet.write(0,0, "Test")
erjiList = []
for i in range(1,nrows):
erjiList.append(sh.cell_value(i,10))
dict = {}
for key in erjiList:
dict[key] = dict.get(key, 0) + 1
aa = 2;
bb = 0;
allCount = 0;
sheet.write(1,0,"二级名称")
sheet.write(1,1,"计数")
for i in dict:
sheet.write(aa,bb,i)
bb += 1
sheet.write(aa,bb,dict[i])
allCount += dict[i]
bb -= 1
aa += 1
aa += 1
sheet.write(aa,bb,"总计")
bb += 1
sheet.write(aa,bb,allCount)
wt.save("数据\\" + fileName + "二级.xls") # 保存
#积压量透视
def thirdStepII():
bk = xlrd2.open_workbook("数据\\" + fileName + "二级.xls", formatting_info = True) # 打开文件
wt = xlutils.copy.copy(bk) # 复制 PS:到这里开始报错了list index out of range
sh=bk.sheet_by_index(2) #使用wlrd读取sheet3
nrows=sh.nrows #获取行数
ncols=sh.ncols #获取列数
sheet = wt.get_sheet(8) # wlwt读取sheet9
# 向单元格写入内容
#sheet.write(0,0, "Test")
erjiList = []
for i in range(1,nrows):
erjiList.append(sh.cell_value(i,10))
dict = {}
for key in erjiList:
dict[key] = dict.get(key, 0) + 1
aa = 2;
bb = 0;
allCount = 0;
sheet.write(1,0,"二级名称")
sheet.write(1,1,"计数")
for i in dict:
sheet.write(aa,bb,i)
bb += 1
sheet.write(aa,bb,dict[i])
allCount += dict[i]
bb -= 1
aa += 1
aa += 1
sheet.write(aa,bb,"总计")
bb += 1
sheet.write(aa,bb,allCount)
wt.save("数据\\" + fileName + "二级.xls") # 保存