The information that each student fills out is automatically rolled up to a new Excel table after the check.
#-*- encoding: utf-8 -*-' Primary school Student Status summary (with ID number validity check) Copyright: gpl owner:[email protected] "import os#import xlrdimport xlwtfrom xlrd import open_workbook from xlutils.copy import copy import reimport tracebackdef check_id ( idcard ): " Check the validity of the ID number ' s = idcard[:-1 ' # "34052419800101001" #分组 temp = zip (s[0:17], [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8,  4, 2]) #相乘 temp2 = Map (Lambda x:int (x[0]) *x[1], temp) #print temp2 #相加 temp3 = sum (TEMP2) #或者这样写: #temp3 = reduce (LAMBDA X, Y : X + Y, TEMP2) print temp3 #最终结果 tmp = ' 10x98765432 ' res = tmp[temp3 % 11] if idcard[ -1] != res: raise exception ("Wrong ID number") def read_one_ file (targetfile,sheetname): #打开excel文件, formatting_info=true can specify save format data = open_workbook (Filename=targetfile,formatting_info=true) #table = data.sheets () [0] #通过索引顺序获取 #table = data.sheet_by_index (0) #通过索引顺序获取 table = data.sheet_by_name ( sheetName ) #通过名称获取 # #读取有意义的内容 row_value = table.row_values (1) ## TODO should check ## place of birth, domicile, Keep Code only: def addr_code ( row_value, index): ## do not check for if row_value[if it is empty index]: row_value[index] = re.search ("\d+", Row_value[index]). Group () try: addr _code (row_value, 4) addr_code (row_value, 14) &nbsP; addr_code (row_value, 51) addr_code (row_value , 63) ## ID Number Check, 12th check_id ( row_value[12] ) #班级检查 assert row_value[15] == "2015102" #入学年份检查 assert row_value[16] == "201509" #入学方式: assert row_value[17] == "Nearest Admission" assert row_ value[18] == print (row_value) except&nbSp Exception as e: print (Traceback.format_exc () ) print ("Error", filename) print (e) #for index , value in enumerate (row_value): #if "130104200905133362" in value: #print (index) #if ("000000" in value): # print (Index, value) return row_value dEf read_files ( dir_path ): for filename in os.listdir ( Path=dir_path): if filename.endswith ("xls"): file_path = os.path.join (dir_path, FileName) yield read_one_file (file_path, sheetname= "Student Base Information") def write2file (Targetfile,dir_path): #w  = XLWT. Workbook () #ws = w.add_sheet (' Student base information ') # #dir_path = r ' H:\Work\Python\xueji\app\input_files ' #for Row_index, row in enumerate ( read_files (dir_path) ): #for col_index , col_value in enumerAte (Row): #ws. Write (Row_index, col _index, col_value ) #注意单元格坐标从 (0,0) Start #w. Save (TargetFile) #import csv #with open ( targetfile, mode= "w") as f: # Writer = csv.writer ( f ) #for row_ Index, row in enumerate ( read_files (dir_path) ): #writer. Writerow (Row) from openpyxl import Workbook wb = Workbook () # grab the active worksheet ws = Wb.active for row_index, row in enumerate ( read_files (Dir_path ) ): #writer. Writerow (Row) # Rows can also be Appended ws.append ( row ) Wb.save ("./output/output.xlsx") if __name__ == "__main__": Dir_path = r ' H:\Work\Python\xueji\app\input_files ' write2file ( /output /Status Template The latest official statistics table. csv ", dir_path) #read_files (Dir_path)
Primary School student Status information summary processing code