使用python讀寫excel

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.