name | method | url | data | json | result |
---|---|---|---|---|---|
get接口 | get | https://httpbin.org/get?a=1b=2 | |||
post表單接口 | post | https://httpbin.org/post | {name: Kevin,age:1} | ||
post-json接口 | post | https://httpbin.org/post | {name: Kevin,age: 21} |
import openpyxl # 打開excel excel = openpyxl.load_workbook('apis.xlsx') # 有路徑應(yīng)帶上路徑 # 使用指定工作表 sheet = excel.active # 當(dāng)前激活的工作表 # sheet = excel.get_sheet_by_name('Sheet1') # 讀取所有數(shù)據(jù) print(list(sheet.values)) # sheet.values 生成器 print(sheet.max_column) # 最大列數(shù) print(sheet.max_row) # 最大行數(shù)
顯示結(jié)果:
[('name', 'method', 'url', 'headers', 'data', 'json', 'result'), ('get接口', 'get', 'https://httpbin.org/get?a=1b=2', None, None, None, None), ('post表單接口', 'post', 'https://httpbin.org/post', 'cookie: token=123', '{name: Kevin,age: 21}', None, None), ('post-json接口', 'post', 'https://httpbin.org/post', None, None, '{name: Kevin,age: 21}', None)]
7
4
代碼接上例
... # 按行讀取 for row in sheet.iter_rows(min_row=1, min_col=1, max_col=3, max_row=3): print(row) # 讀取標(biāo)題行 for row in sheet.iter_rows(max_row=1): title_row = [cell.value for cell in row] print(title_row) # 讀取標(biāo)題行以外數(shù)據(jù) for row in sheet.iter_rows(min_row=2): row_data = [cell.value for cell in row] print(row_data)
打印結(jié)果:
(Cell 'Sheet1'.A1>, Cell 'Sheet1'.B1>, Cell 'Sheet1'.C1>)
(Cell 'Sheet1'.A2>, Cell 'Sheet1'.B2>, Cell 'Sheet1'.C2>)
(Cell 'Sheet1'.A3>, Cell 'Sheet1'.B3>, Cell 'Sheet1'.C3>)
['name', 'method', 'url', 'headers', 'data', 'json', 'result']
['get接口', 'get', 'https://httpbin.org/get?a=1b=2', None, None, None, None]
['post表單接口', 'post', 'https://httpbin.org/post', 'cookie: token=123', '{name: Kevin,age: 21}', None, None]
['post-json接口', 'post', 'https://httpbin.org/post', None, None, '{name: Kevin,age: 21}', None]
代碼接上例
... # 讀取單元格數(shù)據(jù) print(sheet['A1'].value) print(sheet.cell(1,1).value) # 索引從1開始
打印結(jié)果:
name
name
代碼接上例
# 寫入單元格 sheet['F2'] = 'PASS' result_col = title_row.index('result')+1 # 'result'所在的列號(hào) sheet.cell(3, result_col).value = 'PASS' # 整行寫入 new_row = ['post-xml接口', 'post', 'https://httpbin.org/post'] sheet.append(new_row) # 保存文件,也可覆蓋原文件 excel.save("apis2.xlsx")
寫入結(jié)果:
name | method | url | data | json | result |
---|---|---|---|---|---|
get接口 | get | https://httpbin.org/get?a=1b=2 | PASS | ||
post表單接口 | post | https://httpbin.org/post | {name: Kevin,age:1} | PASS | |
post-json接口 | post | https://httpbin.org/post | {name: Kevin,age: 21} | ||
post-xml接口 | post | https://httpbin.org/post |
更多操作可參考官方文檔: https://openpyxl.readthedocs.io/en/stable/
到此這篇關(guān)于Python3利用openpyxl讀寫Excel文件的文章就介紹到這了,更多相關(guān)Python3用openpyxl讀寫Excel文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:德宏 廊坊 長(zhǎng)春 臨汾 漢中 重慶 東莞 河池
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python3利用openpyxl讀寫Excel文件的方法實(shí)例》,本文關(guān)鍵詞 Python3,利用,openpyxl,讀寫,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。