標籤:util 使用 blog row log data 格式 table vlookup
項目中要在excel要跨活頁簿根據一列資料擷取另一列的資料,而excel本身的函數vlookup一直不太好用,只能用程式進行處理了,最近剛接觸了python,靈機一動使用Python進行處理,先將json格式處理成csv格式,再儲存為excel,由於工作日報中要根據之前的json資料進行統計,資料行較大,人工進行工作量較大,然後使用python根據excel的內容進行處理。
import xlrdimport xlwtfrom xlutils.copy import copyimport openpyxlsheetName=‘20170716‘dailyName=‘工作日報‘dailyData=xlrd.open_workbook(dailyName+‘.xlsx‘)dailySheets=dailyData.sheets()dailySheetNames=dailyData.sheet_names()index=dailySheetNames.index(sheetName)#print(index)#擷取sheet索引# for sheet in dailySheetNames:# print(sheet)dailyTable=dailySheets[index]dailyRows=dailyTable.nrowsdailyCols=dailyTable.ncols#print(dailyRows)#日報文檔data = xlrd.open_workbook(sheetName+‘.xlsx‘)table = data.sheets()[0]nrows = table.nrows #行數ncols = table.ncols #列數wb=openpyxl.load_workbook(dailyName+‘.xlsx‘)sheet=wb.get_sheet_by_name(sheetName)#newWs=newWb.get_sheet(index)#oldWb.save(‘test123.xlsx‘)for i in range(0,dailyRows): for j in range(0, nrows): dailyRowValues = dailyTable.row_values(i) rowValues = table.row_values(j) # 某一行資料 #print(‘1‘+rowValues[0]) #print(‘2‘+dailyRowValues[4]) #print(dailyRowValues[4]==rowValues[0]) if dailyRowValues[4]==rowValues[0]: print(i) # for item in rowValues: sheet[‘G‘+str(i+1)]=rowValues[1] sheet[‘H‘+str(i+1)]=rowValues[2] if rowValues[2].find(‘無匹配保單資料‘)>-1: sheet[‘I‘ + str(i + 1)] = ‘是‘ else: if rowValues[2].find(‘無符合查詢條件的保單資料‘)>-1: sheet[‘I‘ + str(i + 1)] = ‘是‘ # print(item)wb.save(dailyName+‘.xlsx‘)
使用python讀寫excel