Python一步步帶你操作Excel
?數(shù)據(jù)處理是 Python 的一大應用場景,而 Excel 則是最流行的數(shù)據(jù)處理軟件。因此用 Python 進行數(shù)據(jù)相關的工作時,難免要和 Excel 打交道。Python處理Excel 常用的系列庫有:xlrd、xlwt、xlutils、openpyxl
?xlrd - 用于讀取 Excel 文件,支持.xls和.xlsx格式
?xlwt - 用于寫入 Excel 文件,只支持.xls格式
?xlutils - 操作 Excel 文件的實用工具,如復制、分割、篩選等
?openpyxl - 既可以讀文件、也可以寫文件、也可以修改文件;但是,openpyxl 庫不支持 xls 格式的Excel文檔。
一、安裝庫的操作
打開cmd,輸入命令進行安裝:pip install xlwt

打開cmd,輸入命令進行安裝:pip install xlrd

打開cmd,輸入命令進行安裝:pip install openpyxl

二、xlwt庫使用
?xlwt - 用于寫入 Excel 文件,只支持.xls格式
1.需求:創(chuàng)建一個新的xls文件中寫入如下數(shù)據(jù),然后保存為login.xls

2.使用xlwt寫入數(shù)據(jù)的步驟
1)導包:import xlwt
2)創(chuàng)建一個文件對象:book=xlwt.Workbook()
3)添加一個sheet工作表:sh1=book.add_sheet(Sheetname)
4)添加內(nèi)容:sh1.write(row,col,value) #單元格行和列分別從0開始
5)保存文件:book.save(filename)
3.代碼實現(xiàn)
# coding = utf-8
import xlwt
#創(chuàng)建一個excel文件對象
book = xlwt.Workbook()
#添sheet工作表
sh1 = book.add_sheet('登錄數(shù)據(jù)')
sh1.write(0,0,'用戶名') # 在A1單元格寫入數(shù)據(jù)
sh1.write(0,1,'密碼') # 在B1單元格寫入數(shù)據(jù)
row1 = ['test','test123']
# 結合循環(huán)寫入一行數(shù)據(jù)
for i in range(len(row1)):
sh1.write(1,i,row1[i])
book.save('login.xls') # 保存文件三、xlrd庫使用
?xlrd - 用于讀取 Excel 文件,支持.xls和.xlsx格式
1.需求:讀取login.xls文件中指定的單元格、指定行、指定的列或者所有的數(shù)據(jù)
2.使用xlrd讀取數(shù)據(jù)的步驟
1)導包:import xlrd
2)打開一個文件:book=xlrd.open_workbook(filename)
3)使用sheet工作表:sh1=book.sheet_by_name(sheet_name)
4)讀取sheet工作表的屬性信息
print('sheet總行數(shù)',sh1.nrows)
print('sheet總列數(shù)',sh1.ncols)
5)讀取sheet工作表存儲的文本內(nèi)容
1)讀取一行:row1=sh1.row_values(row) # 行號從0開始
2)讀取一列:col1=sh1.col_values(col) # 列號從0開始
3)讀取一個單元格:cell_value=sh1.cell(row,col).value
3.代碼實現(xiàn)
# coding = utf-8
import xlrd
book = xlrd.open_workbook('login.xls')
sh1 = book.sheet_by_name('登錄數(shù)據(jù)')
# 讀取第一行的數(shù)據(jù)
row1 = sh1.row_values(0)
print('第一行數(shù)據(jù):',row1)
# 讀取第一列的數(shù)據(jù)
col1 = sh1.col_values(0)
print('第一列數(shù)據(jù):',col1)
# 讀取指定單元格的數(shù)據(jù)
cell = sh1.cell(1,1).value
print('A2單元格的值:',cell)
# 讀取所有的數(shù)據(jù)
rows = sh1.nrows # 獲取當前工作表總的行數(shù)
for i in range(rows):
print('所有數(shù)據(jù)打印,第{}行,數(shù)據(jù)為:{}:'.format(i,sh1.row_values(i)))4.代碼運行結果展示:

四、openpyxl庫使用-寫入數(shù)據(jù)
?openpyxl - 既可以讀文件、也可以寫文件、也可以修改Excel文件;但是不支持 xls 格式
1.需求:對已存在的test_api.xlsx文件寫入接口測試結果,如下圖所示

2.使用openpyx寫入數(shù)據(jù)的步驟
1)導包:import openpyxl
2)打開文件:book = openpyxl.load_workbook(filename)
3)使用sheet工作表:sheet = book[sheetname]
4) 單元格寫入:sh1['F2'] = 'PASS' 或者 sh1.cell(row,col).value='FAIL' #行和列的索從1開始
6:保存文件:book.save(filename)
3.代碼實現(xiàn)
# coding = utf-8
import openpyxl
# 打開excel文件
book = openpyxl.load_workbook('test_api.xlsx')
# 通過工作表名字打開工作表
sh1 = book['register']
# 通過單元格的名稱寫入數(shù)據(jù)
sh1['I2'] = '不通過'
# 通過單元格的行、列寫入數(shù)據(jù)
sh1.cell(3,9).value = '通過'
# 保存文件
book.save('test_api.xlsx')五、openpyxl庫使用-讀取數(shù)據(jù)
1.需求:讀取test_api.xls文件中l(wèi)ogin工作表指定的單元格、指定行、或者所有的數(shù)據(jù)
2.使用openpyx讀取數(shù)據(jù)的步驟
1)導包:import openpyxl
2)打開文件:book = openpyxl.load_workbook(filename)
3)使用sheet工作表:sheet = book[sheetname]
4)讀取sheet工作表的屬性信息
返回工作表的最大行數(shù):sheet.max_row
返回工作表的的最大列數(shù):sheet.max_column
5)讀取sheet工作表存儲的文本內(nèi)容
1)按單元格讀?。篶ell1 = sh1['A1'].value 或者 cell2= sh1.cell(row,col).value #行和列的索引值是從1開始的
2) 按行讀取
for row in sheet.iter_rows(max_row=3):# 讀取前3行數(shù)據(jù)
for cell in row:
print(cell.value,end='\t')
print()
3.代碼實現(xiàn)
# coding = utf-8
import openpyxl
book = openpyxl.load_workbook('test_api.xlsx')
sh1 = book['login']
# 讀取單元格數(shù)據(jù)
cell1 = sh1['A1'].value
print('A1單元格的值為:',cell1)
cell2 = sh1.cell(1,2).value
print('B1單元格的值為:',cell2)
# 讀取前2行數(shù)據(jù)
print('讀取前2行數(shù)據(jù):')
for row in sh1.iter_rows(max_row= 2): # 讀取前2行數(shù)據(jù)
for cell in row:
print(cell.value,end='\t|\t') # 不換行輸出這一行中每個單元格的值
print() # 輸出完一行之后換行
# 讀取所有的數(shù)據(jù)
print('讀取所有的數(shù)據(jù):')
rows = sh1.max_row # 獲取當前工作表總的行數(shù)
for row in sh1.iter_rows(max_row=rows): # 讀取所有的數(shù)據(jù)
for cell in row:
print(cell.value, end='\t|\t') # 不換行輸出這一行中每個單元格的值
print() # 輸出完一行之后換行4、運行結果

到此這篇關于Python一步步帶你操作Excel的文章就介紹到這了,更多相關Python Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Python批量處理Excel文件并轉(zhuǎn)為csv文件示例
這篇文章主要介紹了使用Python批量處理Excel文件并轉(zhuǎn)為csv文件示例,文中通過代碼示例給大家介紹非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2023-12-12
基于python利用Pyecharts使高清圖片導出并在PPT中動態(tài)展示
這篇文章主要介紹了基于python利用Pyecharts使高清圖片導出并在PPT中動態(tài)展示,pyecharts?是一個用于生成?Echarts?圖表的類庫。Echarts?是百度開源的一個數(shù)據(jù)可視化?JS?庫,下面來看看具體的實現(xiàn)過程吧,需要的小伙伴也可以參考一下2022-01-01
python簡單幾步獲取各種DOS命令顯示的內(nèi)容詳解流程
你會用python獲取各種DOS命令顯示的內(nèi)容核心嗎?說的可不是返回值,是用system()函數(shù)調(diào)用windows操作系統(tǒng)的DOS命令來做點事情,需要的朋友可以參考下2021-10-10
python GUI庫圖形界面開發(fā)之PyQt5單選按鈕控件QRadioButton詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5單選按鈕控件QRadioButton詳細使用方法與實例,需要的朋友可以參考下2020-02-02

