1. #!/bin/env python
    2. #encoding=utf-8
    3. import xlrd
    4. import xlwt
    5. from pathlib import Path, PurePath
    6. #导入excel和文件操作库
    7. #指定要合并excel的路径
    8. src_path = '/Users/chixin/Documents/学习资料/python文件处理/文章1代码/调查问卷'
    9. #指定合并完成的路基ing
    10. dst_file = '/Users/chixin/Documents/学习资料/python文件处理/文章1代码/result/结果-20210222.xlsx'
    11. #取该目录下所有的xlsx格式文件
    12. p = Path(src_path)
    13. files = [x for x in p.iterdir() if PurePath(x).match('*.xlsx')]
    14. #准备一个列表存放读取结果
    15. content = []
    16. #对每一个文件进行重复处理
    17. for file in files:
    18. #用户名作为每个用户的标识
    19. username = file.stem
    20. data = xlrd.open_workbook(file)
    21. table = data.sheets()[0]
    22. #取得每一项的结果
    23. answer1 = table.cell_value(rowx=4, colx=4)
    24. answer2 = table.cell_value(rowx=10, colx=4)
    25. temp = f'{username},{answer1},{answer2}'
    26. #合并为一行先存储
    27. content.append(temp.split(','))
    28. print(temp)
    29. #输出
    30. #准备写入文件的表头
    31. table_header = ['员工姓名', '第一题', '第二题']
    32. workbook = xlwt.Workbook(encoding='utf-8')
    33. xlsheet = workbook.add_sheet("统计结果")
    34. # 写入表头
    35. row = 0
    36. col = 0
    37. for cell_header in table_header:
    38. xlsheet.write(row, col, cell_header)
    39. col += 1
    40. #向下移动一行
    41. row += 1
    42. #取出每一行内容
    43. for line in content:
    44. col = 0
    45. #取出每个单元格内容
    46. for cell in line:
    47. #写入内容
    48. xlsheet.write(row,col,cell)
    49. #向右移动一个单元格
    50. col += 1
    51. #向下移动一行
    52. row += 1
    53. #保存最终结果
    54. workbook.save(dst_file)