import os from openpyxl import Workbook from openpyxl import load_workbook os.system("DIR *.* /B>list.txt") f = open("list.txt", "r") name=[] while True: line = f.readline() if line: line=line[0:len(line)-1] #delete char '\n' #print (line) if line.endswith('xlsx',len(line)-4,len(line) ): name.append(line) else: break if len(name)==0: print('No .xlsx file in this directory') exit() outbook = Workbook() #输出book对象 outsheet = outbook.active #获取book的sheet rbook = load_workbook( name[0] ) rsheet = rbook.get_sheet_names() sheet1 = rbook.get_sheet_by_name(rsheet[0]) for j in range(1,12): outsheet.cell(row=1,column=j).value = sheet1.cell(row=1,column=j).value outr=2 for n in name: print('name: ',end='') print(n) rbook = load_workbook( n ) rsheet = rbook.get_sheet_names() sheet1 = rbook.get_sheet_by_name(rsheet[0]) for i in range(1,9): if sheet1.cell(row=i,column=2).value==None and not sheet1.cell(row=i,column=3).value == None: for j in range(1,10): outsheet.cell(row=outr,column=j).value = sheet1.cell(row=i,column=j).value print ( sheet1.cell(row=i,column=j).value ) break; outr=outr+1 outbook.save('out.xlsx') #保存输出