work=xlwt.Workbook('F:\PyCharm\python\EXCLLSEARCH\EXCELTEST.xlsx') #建立一个文件
path='F:\PyCharm\python\EXCLLSEARCH\WJJ' #目录(放Excel表格的目录)
file_list=os.listdir(path)
data_lst = []
for file in file_list: #循环遍历列出所有文件名称
file_name = os.path.join(path,file) #路径+文件名
workbook=xlrd.open_workbook(file_name) #打开第一个文件
sheet_name=workbook.sheet_names() #获取第一个文件的sheet名称
for file_1 in sheet_name: #循环遍历每个sheet
val=[]
if (file_1 not in data_lst):
sheet=work.add_sheet(file_1,cell_overwrite_ok=True) #新建一个sheet
data_lst.append(file_1)
table=workbook.sheet_by_name(file_1) #以名字为索引
rows= table.nrows #获取sheet行数
clos=table.ncols #获取sheet列数目
for i in range(rows): #循环遍历没一行
val.append(table.row_values(i)) #获取没一行的值
for x in range(len(val)):
for y in range(len(val[x])):
sheet.write(x,y,val[x][y])
work.save('F:\PyCharm\python\EXCLLSEARCH\EXCELTEST.xlsx')[/quote]
这样发现重名sheet就会弹出错误,而且不会追加内容只能覆盖
work=xlwt.Workbook('F:\PyCharm\python\EXCLLSEARCH\EXCELTEST.xlsx') #建立一个文件
path='F:\PyCharm\python\EXCLLSEARCH\WJJ' #目录(放Excel表格的目录)
file_list=os.listdir(path)
data_lst = []
for file in file_list: #循环遍历列出所有文件名称
file_name = os.path.join(path,file) #路径+文件名
workbook=xlrd.open_workbook(file_name) #打开第一个文件
sheet_name=workbook.sheet_names() #获取第一个文件的sheet名称
for file_1 in sheet_name: #循环遍历每个sheet
val=[]
if (file_1 not in data_lst):
sheet=work.add_sheet(file_1,cell_overwrite_ok=True) #新建一个sheet
data_lst.append(file_1)
table=workbook.sheet_by_name(file_1) #以名字为索引
rows= table.nrows #获取sheet行数
clos=table.ncols #获取sheet列数目
for i in range(rows): #循环遍历没一行
val.append(table.row_values(i)) #获取没一行的值
for x in range(len(val)):
for y in range(len(val[x])):
sheet.write(x,y,val[x][y])
work.save('F:\PyCharm\python\EXCLLSEARCH\EXCELTEST.xlsx')