import os import win32com.client as win32 import shutil import openpyxl #####最初のステップは、xlsxファイルを元のフォルダーにドロップし、それをbakフォルダーに直接コピーすること です。最初のファイルをスライス= [] path = os.getcwd ()#変換前にプログラムが配置されているファイルディレクトリ path_bak = os.getcwd()+ r '/ bak /' pathdir = os.listdir(path) print(pathdir) for s in pathdir: newdir = os.path。 join(path、s) if os.path.isfile(newdir): os.chdir(path_bak) pathdir_bak = os.listdir(path_bak) #print(pathdir_bak) if os.path.splitext(newdir)[1] == " .xls ": t = os.path.split(s)[1] + r'x '#print os.path.exists(t): #print (os.path.exists(os.path.split(s)[1])) break else:excel = win32.gencache.EnsureDispatch( 'Excel.Application') wb = excel.Workbooks.Open(newdir) # print(newdir) path_dir = os.path.split(newdir)[0] path_file = os.path.split(newdir)[1] #print (path_dir) #print(path_file )wb.SaveAs(path_dir + '/ bak / '+ path_file +' x '、FileFormat = 51)#FileFormat = 51は.xlsx拡張子用 wb.Close()#FileFormat = 56は.xls拡張子用 excel.Application.Quit() elifos.path.splitext(newdir)[1] == ".xlsx": file.append(newdir) elif os.path.isdir(newdir): 継続する for i in range(len(file)): pcm_file = file [ i] path_0 = os.path.split(file [i])[0] path_1 = os.path.split(file [i])[1] oldname = path_0 + '/' + path_1 newname = path_0 + r '/ bak / '+ path_1 shutdown.copyfile(oldname、newname) ##### 2番目のステップは、ファイルディレクトリをbakフォルダーに切り替え、 uvw file_deal = [] path_deal = os.getcwd()#プログラムの場所をスライスすることです。変換前 です pathdir_bak内のuのファイルディレクトリ pathdir_deal = os.listdir(path_deal):newdir_deal = os.path.join(path_deal、u) file_deal.append(newdir_deal) #print(file_deal) for j in range(len(file_deal)): pcm_file_deal = file_deal [j] path_0_deal = os.path.split(file_deal [j])[0]#文件所在目录 sheet_selected = wb.get_sheet_by_name(allSheets [i]) path_1_deal = os.path.split(file_deal [j])[1]#文件名带后缀#print (pcm_file_deal) #print(path_0_deal) print(path_1_deal) wb = openpyxl.load_workbook(path_1_deal) allSheets = wb.get_sheet_names() for i in range(len(allSheets)): sheet = wb.get_sheet_by_name(allSheets [i]) for row in sheet.iter_rows(): for cell in row: if str(str(cell.value).replace( '' 、 ''))。find( "资生成负债表")!= -1: print(sheet_selected) break ws = wb.get_sheet_by_name(sheet_selected.title) print(ws)
テーブルの論理ループを取得する
おすすめ
転載: blog.csdn.net/jidawanghao/article/details/112680178
おすすめ
ランキング