excel - Nested loops, looping through worksheets and looping through workbooks Python -
i loop on each workbook in folder , each worksheet in workbook. when run sample below however, first worksheet in each workbook used. therefore not active worksheet, if there hidden worksheets in order first worksheet used in loop below. how can loop on each worksheet in each workbook correctly?
for contextualising use of loop, loop should work through each file specified in allfiles code should work on each sheet in each workbook. sets index, strips index, squeezes nan, obtains rows based on staff , total staff labels, removes blank rows , adds source filename of workbook. appended list concatenated single dataframe. therefore should extract same type of data each worksheet , combine them single structure.
for file_ in allfiles: wb = load_workbook(file_) sheet in wb.worksheets: ws = sheet parsed_file1 = dataframe(ws) try: parsed_file1.set_index(parsed_file1.columns[0], inplace = true) parsed_file1.index.str.strip() cd3 = parsed_file1.apply(squeeze_nan, axis=1) staff3_ = cd3.loc["staff" : "total staff"].copy() staff3_.dropna(axis=0, thresh=2, inplace = true) staff3_['filename'] = os.path.basename(file_) list4.append(staff3_) except: pass
Comments
Post a Comment