詳解Python openpyxl庫的基本應(yīng)用
1、導(dǎo)入文件 wb(可自定義) = openpyxl.load_workbook(#輸入文件位置#) 2、轉(zhuǎn)換為可處理的對象 sheet(可自定義)= wb[’表格中對應(yīng)的那一張的名稱’]
3、sheet.cell(row=i, column=j) .value 可以顯示對應(yīng)單元格的值4. wb.save[’位置’] 保存表格
’’’ # Created by Hailong Liu # for work # 2020.11.21’’’import openpyxl#導(dǎo)入表格wb = openpyxl.load_workbook(’E:/處理.xlsx’)#存入一個(gè)可處理的對象中sheet = wb[’處理數(shù)據(jù)’]lst_time = [sheet.cell(row = i, column = 1).value for i in range(1,17270)]lst_flow = [sheet.cell(row = i, column = 2).value for i in range(1,17270)]lst_length = [sheet.cell(row = i, column = 3).value for i in range(1,17270)]lst_arrive = []#計(jì)算到達(dá)量for i in range(1,len(lst_flow)): rz = lst_flow[i] - lst_length[i] if rz >= 0: lst_arrive.append(lst_flow[i]) else: lst_arrive.append(lst_length[i]-lst_length[i-1]+lst_flow[i])#輸出驗(yàn)證for i in range(0,len(lst_flow)-1): print(lst_arrive[i])#添加到表格中并保存sheet[’D1’] = '到達(dá)量'for i in range(2,len(lst_arrive)+1): sheet.cell(row = i, column = 4).value = lst_arrive[i-1]# wb.save(’E:/處理(改).xlsx’)
知識(shí)點(diǎn)擴(kuò)展:
python3 openpyxl庫的簡單使用
python3操作表格有很多庫,現(xiàn)在主要給大家介紹一下我比較喜歡用的openpyxl庫,安裝直接pip安裝,對pip安裝有疑問可以參考我有關(guān)于pip使用的文章。
wb=Workbook()#新建表格wb.save(filename='')#保存表格wb=load_workbook()#打開已有表格ws=wb.active#選取當(dāng)前表格活躍的sheetws.wb[]#根據(jù)sheetname打開sheetws=wb.creat_sheet(title=’’)#新建一個(gè)sheetws.cell(row=1,column=1).value=’’#往ws這個(gè)sheet第一行第一列寫入ws.max_column#獲取最大列數(shù)ws.cell(row=1,column=1).value#獲取第一行第一列的值ws[’A1’].column#獲取該數(shù)據(jù)列數(shù)
到此這篇關(guān)于詳解Python openpyxl庫的基本應(yīng)用的文章就介紹到這了,更多相關(guān)Python openpyxl庫內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
