#!/bin/env python#encoding=utf-8import xlrdimport xlwtfrom pathlib import Path, PurePath#导入excel和文件操作库#指定要合并excel的路径src_path = '/Users/chixin/Documents/学习资料/python文件处理/文章1代码/调查问卷'#指定合并完成的路基ingdst_file = '/Users/chixin/Documents/学习资料/python文件处理/文章1代码/result/结果-20210222.xlsx'#取该目录下所有的xlsx格式文件p = Path(src_path)files = [x for x in p.iterdir() if PurePath(x).match('*.xlsx')]#准备一个列表存放读取结果content = []#对每一个文件进行重复处理for file in files: #用户名作为每个用户的标识 username = file.stem data = xlrd.open_workbook(file) table = data.sheets()[0] #取得每一项的结果 answer1 = table.cell_value(rowx=4, colx=4) answer2 = table.cell_value(rowx=10, colx=4) temp = f'{username},{answer1},{answer2}' #合并为一行先存储 content.append(temp.split(',')) print(temp) #输出#准备写入文件的表头table_header = ['员工姓名', '第一题', '第二题']workbook = xlwt.Workbook(encoding='utf-8')xlsheet = workbook.add_sheet("统计结果")# 写入表头row = 0col = 0for cell_header in table_header: xlsheet.write(row, col, cell_header) col += 1#向下移动一行row += 1#取出每一行内容for line in content: col = 0 #取出每个单元格内容 for cell in line: #写入内容 xlsheet.write(row,col,cell) #向右移动一个单元格 col += 1 #向下移动一行 row += 1#保存最终结果workbook.save(dst_file)