|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大神好,小弟需要删掉如图多个excel 中某个指定名字的sheet,代码如下,运行的时候一直报错,求大神指点!
- import openpyxl
- import os
- def sheet_delete(excel_path,sheet_name):
- wb = openpyxl.load_workbook(excel_path)
- ws = wb[sheet_name]
- wb.remove(ws)
- wb.save(excel_path)
- path = r'C:\remove sheet'
- files = os.listdir(path)
- sheet_name = 'Parameter'
- for i in (1,100):
- excel_path = r'C:\remove sheet'
- sheet_delete(excel_path,sheet_name)
复制代码
这样试试
- import openpyxl
- import os
- def sheet_delete(excel_path, sheet_name):
- wb = openpyxl.load_workbook(excel_path)
- if sheet_name in wb.sheetnames:
- ws = wb[sheet_name]
- wb.remove(ws)
- wb.save(excel_path)
- path = r'D:\python\test'
- files = os.listdir(path)
- sheet_name = 'Sheet2'
- for i in files:
- if os.path.splitext(i)[1] == '.xlsx':
- excel_path = path + os.sep + i
- print(excel_path)
- sheet_delete(excel_path, sheet_name)
复制代码
|
-
excel名字没有规律,但是需要删掉的sheet 名字相同
|