Python操作Excel(openpyxl)
1.引入openpyxl库
安装openpyxl库:pip install openpyxl
引入openpyxl库:from openpyxl import load_worbook
2.代码实现
from openpyxl import load_workbook #打开Excel wb = load_workbook("C:\Users\Administrator\Desktop\testdemo.xlsx") #定位表单 sheet = wb["s1"] #定位单元格 行列值 print("获取最大行数:",sheet.max_row) print("获取最大列数:",sheet.max_column) # #遍历 test_list = []#列表 for a in range(2,sheet.max_row+1): test_dic = {} # 字典 for b in range(1,sheet.max_column+1): #获取指定单元格的值:sheet.cell(行,列).value #将获取到的数据添加到字典 test_dic["method"]=sheet.cell(a,1).value test_dic["url"]=sheet.cell(a,2).value test_dic["data"]=eval(sheet.cell(a,3).value)#eavl() 把数据类转换成 原本数据类型 test_dic["expect"]=sheet.cell(a,4).value test_list.append(test_dic)#将字典添加到列表 print(test_list)
输出:
最大行数: 3 最大列数: 4 [{"method": "get", "url": "http://www.qabujiaban.com/user/login", "data": {"username": "uuuu222都44", "password": "WJHasb124*1"}, "expect": "0000"}, {"method": "get", "url": "http://www.qabujiaban.com/user/login", "data": {"username": "uuuu222都44", "password": "WJHasb124*1"}, "expect": "0000"}]