准备工作 完成这项工程需要用到os库与xlrd库
其详细介绍可以参考这两篇博客:
os库篇
xlrd库篇
思路 1.先把excel文件都放入一个文件夹中
2.再读取这个文件夹,把所有excel文件的地址 存到list中
3.再对每一个excel文件进行操作(我求的是某一列数据的平均值)
代码实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 import xlrdimport osclasses = [] classes_name = [] classes_mark = [] origin_address = r"E:\py\marks\mark" marks_dir = os.listdir(origin_address) for i in range (0 , len (marks_dir)): mark_path = os.path.join(origin_address, marks_dir[i]) if os.path.isfile(mark_path): classes.append(mark_path) doc = open ("result.txt" , "w" ) for i in range (0 , len (classes)): classes_name.append(os.path.basename(classes[i])[:-5 ]) now_class = xlrd.open_workbook(classes[i]) now_sheet = now_class.sheets()[0 ] sheet_rows = now_sheet.nrows sheet_cols = now_sheet.ncols mark_sum = 0 students_sum = sheet_rows - 7 for j in range (5 , sheet_rows - 2 ): mark_sum += now_sheet.row(j)[sheet_cols - 2 ].value classes_mark.append(mark_sum / students_sum) print (classes_name[i], ":" , classes_mark[i], flie=doc) doc.close()