往excel新增sheet出错原因

想往一个已经存在的excel中新增sheet,结果发现一直报错。
出错代码如下:
sheet2 = workbook2.add_sheet(“O2_2”)
Traceback (most recent call last):
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site- packages\IPython\core\interactiveshell.py”, line 3331, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
sheet2 = workbook2.add_sheet(“O2_2”)
AttributeError: ‘Book’ object has no attribute ‘add_sheet’
后来发现是需要对workbook进行copy操作:
workbook = xlrd.open_workbook(r’E:\Softwarestudy\A14101.xls’) #打开工作簿
newb=copy(workbook2) #复制成xlwt形式;
wbsheet=newb.add_sheet(“O2_2”) #新建表格

你可能感兴趣的:(工作心得,python,excel)