亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python一步步帶你操作Excel

 更新時間:2022年08月01日 10:23:19   作者:測試界的飄柔  
這篇文章主要介紹了Python編寫命令行腳本操作excel的方法,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下

?數(shù)據(jù)處理是 Python 的一大應(yīng)用場景,而 Excel 則是最流行的數(shù)據(jù)處理軟件。因此用 Python 進行數(shù)據(jù)相關(guān)的工作時,難免要和 Excel 打交道。Python處理Excel 常用的系列庫有:xlrd、xlwt、xlutils、openpyxl

?xlrd - 用于讀取 Excel 文件,支持.xls和.xlsx格式

?xlwt - 用于寫入 Excel 文件,只支持.xls格式

?xlutils - 操作 Excel 文件的實用工具,如復(fù)制、分割、篩選等

?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)導(dǎo)包: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']
# 結(jié)合循環(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)導(dǎo)包: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  # 獲取當(dāng)前工作表總的行數(shù)
for i in range(rows):
    print('所有數(shù)據(jù)打印,第{}行,數(shù)據(jù)為:{}:'.format(i,sh1.row_values(i)))

4.代碼運行結(jié)果展示:

四、openpyxl庫使用-寫入數(shù)據(jù)

?openpyxl - 既可以讀文件、也可以寫文件、也可以修改Excel文件;但是不支持 xls 格式

1.需求:對已存在的test_api.xlsx文件寫入接口測試結(jié)果,如下圖所示

2.使用openpyx寫入數(shù)據(jù)的步驟

1)導(dǎo)包: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)導(dǎo)包: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)按單元格讀取:cell1 = 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  # 獲取當(dāng)前工作表總的行數(shù)
for row in sh1.iter_rows(max_row=rows): # 讀取所有的數(shù)據(jù)
    for cell in row:
        print(cell.value, end='\t|\t') # 不換行輸出這一行中每個單元格的值
    print() # 輸出完一行之后換行

4、運行結(jié)果

到此這篇關(guān)于Python一步步帶你操作Excel的文章就介紹到這了,更多相關(guān)Python Excel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論