回覆 13# ~monkey~
講明先, 我冇學過programming , 只係上網左抄右抄人d script 一知半解咁用, 可能有錯, 仲可能寫得好怪。
話就話一樣格式但係間唔中梗有幾個excel 多一行少一行走晒位所以要逐個cell search :- import os
- import openpyxl ##淨係可以讀寫xlsx
- import xlrd ##淨係可以讀xls, 要寫要用xlwt, openpyxl 同xlrd 都要用pip 另外裝咗先用到
- dirpath = "/path/to/excel/files" ##準備個filelist 先
- FILES = []
- for DIRpaths,DIRnames,FILEnames in os.walk(dirpath):
- for file in FILEnames:
- row = []
- row.append(DIRpaths)
- row.append(file)
- FILES.append(row)
- FILESxls = []
- for DIRpaths, file in FILES: ##filter 走唔關事嘅file
- if (file.find('.xls') >= 0 and file.find('.xlsx') < 0):
- FILESxls.append(os.path.join(DIRpaths,file))
- TABLE = [] ##開個table , 讀完所有data 之後一次過寫入去新xlsx
- for file in FILESxls:
- workbook = xlrd.open_workbook(file)
- worksheet = workbook.sheet_by_index(0) ## 逐個xls 開, read第一張sheet
- num_of_row = worksheet.nrows
- num_of_col = worksheet.ncols ##先check 下有data 嘅範圍有幾大
- ##set 返個limit, 費事有d人喺個xls 度寫到落65535咁就有排讀
- if num_of_row > 100:
- num_of_row = 100
- if num_of_col > 13:
- num_of_col = 13
- for y in range(num_of_row):
- for x in range(num_of_col):
- cell = worksheet.cell_value(y,x) ##從上至下左至右逐個cell 讀, 先讀每個excel 只有一個嘅value
- if isinstance(cell, str):
- if "ABC" in cell:
- ABC = worksheet.cell_value(y+1,x) ##記低寫住"ABC"個cell 對落一格個value
- if "CBA" in cell:
- CBA = []
- CBA.append(x)
- CBA.append(y) ##記低寫住"CBA"個cell 個座標
- if "Date" in cell:
- DATE = worksheet.cell_value(y,x)
- DATE = '=text("'+str(DATE)+'","yyyy-mm-dd")' ##日子唔識搞, 夾硬用excel function 寫入去
- if "1234567" in cell:
- STARTROW = []
- STARTROW.append(x)
- STARTROW.append(y)
- ENDROW = []
- ENDROW.append(1)
- ENDROW.append(num_of_row-1) ##指定某個範圍一陣再讀一次
- for y in range(STARTROW[1]+3,ENDROW[1]-1):
- row = []
- row.append(DATE)
- row.append(ABC)
- row.append(worksheet.cell_value(y,CBA[0])) ##根據之前記低個座標逐行讀落去
- TABLE.append(row) ##逐行寫入table, 寫完loop 返上去去下一個xls
-
- workbook = openpyxl.Workbook() ## 開新xlsx 準備寫入去
- worksheet = workbook.active
- if TABLE:
- n = ('DATE','ABC','CBA')
- for x in range(len(TABLE[0])):
- worksheet.cell(column=x+1,row=1).value = n[x] ##先寫第一行field names
- num_of_head_row = 1
- for y in range(len(TABLE)):
- for x in range(len(TABLE[y])):
- worksheet.cell(column=x+1,row=y+1+num_of_head_row).value = TABLE[y][x] ##跟住由上至下左至右逐個cell 寫入去
- output = "/path/to/output/file"
- workbook.save(output) ##save file, 完
- else:
- print("no TABLE")
複製代碼 |