Reuse this script to read excel in bulk and get the output of all excel sheet to a name of each file.
Environment: python 3.7.3
. 1 # - * - Coding: UTF-. 8 - * - 2 . 3 '' ' . 4 purpose of this is to obtain a plurality of code sheet excl of name, and output to the specified file . 5 ' '' . 6 . 7 Import SYS . 8 Import to xlrd . 9 Import OS 10 Import the logging . 11 12 is 13 is # parameters and configuration functions provided logging.basicConfig logging.basicConfig () method 14 the FORMAT = ' [% (funcName) S:% (lineno) D]:% (Message) S ' 15 the LEVEL = logging.info 16 logging.basicConfig (= the LEVEL Level, the format = the FORMAT) . 17 18 is # 保存输出sheet name 19 output_file = './SheetNameList.txt' 20 21 # init function 22 def get_obj_list(dir_name): 23 filelist = os.listdir(dir_name) 24 for item in filelist : 25 item = dir_name + item 26 if os.path.isfile(item) and ( item[-5:] == '.xlsx' or item[-5:] == '.xlsm'): 27 if item.find("$") != -1 or item.find("csr_example") != -1 : 28 continue 29 GetSheetNameList(item) 30 elif os.path.isdir(item): 31 item = item + '/' 32 new_obj_list = [] 33 new_obj_list = get_obj_list(item) 34 35 36 # get sheet name and save in output_file 37 def GetSheetNameList(excelName): 38 fd = open(output_file, 'a+') 39 fd.write(excelName + '\n') 40 excelfd = xlrd.open_workbook(excelName) 41 for sheetName in excelfd.sheet_names(): 42 fd.write(sheetName + '\n') 43 fd.close() 44 45 46 if __name__ == "__main__": 47 if os.path.exists(output_file): 48 os.remove(output_file) 49 get_obj_list('./excel/')
File Structure: