python3結(jié)合openpyxl庫實(shí)現(xiàn)excel操作的實(shí)例代碼
一.相關(guān)說明:
1、openpyxl(可讀寫excel表)專門處理Excel2007及以上版本產(chǎn)生的xlsx文件;2007一下的版本為xls結(jié)尾的文件,需要使用 xlrd和xlwt庫進(jìn)行操作
2、excel表的文字編碼如果是“gb2312” 讀取后就會(huì)顯示亂碼,請(qǐng)先轉(zhuǎn)成Unicode
3、workbook: 工作簿,一個(gè)excel文件包含多個(gè)sheet。
4、sheet:工作表,一個(gè)workbook有多個(gè),表名識(shí)別,如“sheet1”,“sheet2”等。
5、cell: 單元格,存儲(chǔ)數(shù)據(jù)對(duì)象
二.openpyxl的使用方法
1. 新建
import openpyxl # 新建一個(gè)空excel,表名為sheet,文件名為test wb = openpyxl.Workbook() # 創(chuàng)建新的excel文件,一個(gè)工作簿(workbook)在創(chuàng)建的時(shí)候同時(shí)至少也新建了一張工作表(worksheet) wb.save('test.xlsx')
2.表操作
注:從此操作往后都使用如下的測(cè)試數(shù)據(jù)
import openpyxl import openpyxl.styles # 打開已有的excel wb = openpyxl.load_workbook("test.xlsx") # ------------------ 表操作 ------------------ # 新建sheet表 wb.create_sheet(index=2, title="sheet3") # 可通過index控制創(chuàng)建的表的位置 # 獲取所有表名 sheet_names = wb.sheetnames # 得到工作簿的所有工作表名 結(jié)果: ['Sheet1', 'Sheet2', 'Sheet3'] # 根據(jù)表名刪除sheet表 wb.remove(wb[sheet_names[2]]) # 刪除上一步建的第3個(gè)工作表 # 根據(jù)表名打開sheet表 sheet1 = wb[sheet_names[0]] # 打開第一個(gè) sheet 工作表
3.讀取數(shù)據(jù)
# ------------------ 讀取數(shù)據(jù) ------------------ # 獲取單元格數(shù)據(jù) sheet1_max_colum = sheet1.max_column # 獲取最大列數(shù) 結(jié)果:3 # ws = wb.active # 獲取當(dāng)前活動(dòng)的sheet頁 sheet1_max_row = sheet1.max_row # 獲取最大行數(shù) 結(jié)果:10 A1_value = sheet1['A1'].value # 獲取單元格A1值 結(jié)果:a1 A1_column = sheet1['A1'].column # 獲取單元格A1列值 結(jié)果: A A1_row = sheet1['A1'].row # 獲取單元格A1行號(hào) 結(jié)果: 1 A1 = sheet1.cell(row=1, column=1).value # 獲取第一行第一列的單元格值 結(jié)果:a1 # 獲取C列的所有數(shù)據(jù) list_sheet1_column_C = [] for i in sheet1["C"]: list_sheet1_column_C.append(i.value) # 獲取第1行的所有數(shù)據(jù) list_sheet1_row_1 = [] for i in sheet1[1]: list_sheet1_row_1.append(i.value) # 讀取所有數(shù)據(jù) list_sheet1_all = [] for row in sheet1.rows: for cell in row: list_sheet1_all.append(cell.value) # 按行循環(huán)獲取所有的值,保存在 list_sheet1_all 列表中
4.寫入數(shù)據(jù)
# ------------------ 寫入數(shù)據(jù) ------------------ sheet1.cell(row=1, column=2, value="B1") # 修改第一行第二列的單元格的值為B1 sheet1["A1"] = "A1" # 直接修改A1單元格的值為A1 sheet1["B11"] = "B11" # 新增B11單元格的值為B11 sheet1.title = "test_sheet" # 修改sheet1的表名為test_sheet
5.表格樣式調(diào)整
# ------------------ 表格樣式調(diào)整 ------------------ # 表格樣式支持:字體、顏色、模式、邊框、數(shù)字格式等 # A1單元格 等線24號(hào)加粗斜體,字體顏色淺藍(lán)色 sheet1["B11"].font = openpyxl.styles.Font(name="宋體", size=24, italic=True, color="00CCFF", bold=True) # B1單元格 水平上下居中 sheet1["B11"].alignment = openpyxl.styles.Alignment(horizontal="center", vertical="center") # 第一行高度設(shè)置為30 sheet1.row_dimensions[1].height = 30 # C列的寬度設(shè)置為35 sheet1.column_dimensions["C"].width = 35
6.保存文件
# 保存文件,注意文件打開時(shí)文件保存會(huì)出錯(cuò) wb.save("test.xlsx")
補(bǔ)充:下面通過實(shí)例代碼看下python3 openpyxl基本操作,具體代碼如下所示:
#coding:utf-8 import xlrd import xlwt # 讀寫2007 excel import openpyxl import sys #讀取設(shè)備sn # def readSN(path): # wb = openpyxl.load_workbook(path) # sheet = wb.active # dict = [] # for i in range(2, sheet.max_row +1): # c = sheet["C" + str(i)].value; # d = sheet["D" + str(i)].value; # # dict.append(d) # #dict.append(d) # #print(c,d) # return dict; # # pass; # print(readSN("./sim/1.xlsx")) def read07Excel(path,path1): wb = openpyxl.load_workbook(path) sheet = wb.active # print(sheet.max_column) # 獲取最大列數(shù) # print(sheet.max_row) # 獲取最大行數(shù) #print(sheet['B1'].value) wb1 = openpyxl.load_workbook(path1) sheet1 = wb1.active for i in range(2,sheet.max_row): iccid = sheet["B"+str(i)].value; len_iccid = len(iccid) if len_iccid == 20 : sub_iccid = iccid[16:-1] elif len_iccid == 21: sub_iccid = iccid[17:-1] for x in range(1,sheet1.max_row): #print(sheet1["D"+str(x)].value) if sub_iccid+"N" == sheet1["D"+str(x)].value: sheet["O"+str(i)].value = sheet1["C"+str(x)].value; wb.save(filename=path) print(str(sheet1["D"+str(x)].value) + " "+ str(sheet1["C"+str(x)].value) +" "+ str(iccid)) print() pass # 寫入數(shù)據(jù) # s =sheet["P"+str(i)].value = "dsdaf"; # wb.save(filename=path) # p = sheet["P" + str(i)].value; #print(sub_iccid) # for row in sheet.rows: # for cell in row: # print(cell.value, "\t", end="") # print(cell.column, "\t", end="") # # # print() # sys.exit() # path = "./sim/2.xlsx" # wb = openpyxl.load_workbook(path) # #sheet = wb.sheetnames[0] #獲取名稱 # sheet = wb.active # 分別返回 #print(sheet['A1'].value) #獲取單元格A1值 read07Excel("./sim/2.xlsx","./sim/1.xlsx") # wb=openpyxl.load_workbook('./sim/1.xlsx') #打開excel文件 # print(wb.sheetnames) #獲取工作簿所有工作表名
總結(jié)
以上所述是小編給大家介紹的python3結(jié)合openpyxl庫實(shí)現(xiàn)excel操作,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- python通過openpyxl生成Excel文件的方法
- Python使用openpyxl讀寫excel文件的方法
- python使用openpyxl庫修改excel表格數(shù)據(jù)方法
- python操作excel的包(openpyxl、xlsxwriter)
- Python Excel處理庫openpyxl使用詳解
- Python操作excel的方法總結(jié)(xlrd、xlwt、openpyxl)
- python 使用openpyxl讀取excel數(shù)據(jù)
- Python如何實(shí)現(xiàn)Excel的最合適列寬(openpyxl)
- python使用openpyxl庫處理Excel文件詳細(xì)教程
相關(guān)文章
Python地理地圖可視化folium標(biāo)記點(diǎn)彈窗設(shè)置代碼(推薦)
這篇文章主要介紹了Python地理地圖可視化folium標(biāo)記點(diǎn)彈窗設(shè)置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09pyhton學(xué)習(xí)與數(shù)據(jù)挖掘self原理及應(yīng)用分析
這篇文章主要為大家介紹了深入分析pyhton中的self原理及應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-11-11如何使用Django(python)實(shí)現(xiàn)android的服務(wù)器端
這篇文章主要介紹了Django(python)簡(jiǎn)單實(shí)現(xiàn)android的服務(wù)器端,這里所需要的工具是PyCharm--python編輯工具,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Python實(shí)現(xiàn)將圖片轉(zhuǎn)換為ASCII字符畫
這篇文章主要介紹了Python實(shí)現(xiàn)將圖片轉(zhuǎn)換為ASCII字符畫,要將圖片轉(zhuǎn)換為字符圖其實(shí)很簡(jiǎn)單,我們首先將圖片轉(zhuǎn)換為灰度圖像,這樣圖片的每個(gè)像素點(diǎn)的顏色值都是0到255,然后我們選用一些在文字矩形框內(nèi)占用面積從大到小的ASCII碼字符2022-08-08Python2與Python3的區(qū)別點(diǎn)整理
在本篇文章里小編給大家整理的是關(guān)于Python2與Python3的區(qū)別點(diǎn)整理內(nèi)容,需要的朋友們可以參考下。2019-12-12python轉(zhuǎn)換wrf輸出的數(shù)據(jù)為網(wǎng)頁可視化json格式
這篇文章主要介紹了python轉(zhuǎn)換wrf輸出的數(shù)據(jù)為網(wǎng)頁可視化json格式,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09