#下载安装xlrd pip install xlrd==1.2.0
import xlrd
#打开文件,实例化文件对象
excel_obj = xlrd.open_workbook(r"C:\Users\admin\Desktop\xlrd.xlsx")
#实例化sheet对象,下标是从0开始的
sheet_obj = excel_obj.sheet_by_name("Sheet1") #通过sheet的名字来确定实例化对象
sheet1_obj = excel_obj.sheet_by_index(0) #通过sheet的下标来确定实例化对象
res = sheet_obj.row_values(0) #获取第0行的数据
res1 = sheet_obj.row_values(0,start_colx=1,end_colx=3) #获取第0行的第1,2个的数据,遵循左开右闭
res2 = sheet_obj.col_values(1) #获取第1列的数据
res3 = sheet_obj.col_values(1,1,3) #获取第一列,第1,2行数据
res4 = sheet_obj.cell_value(6,1) #获取第3行第1列的数据
res5 = sheet_obj.nrows #一共有多少行数据
#将表格数据组装成字典,第一列的url除外
keys = sheet_obj.col_values(0,1) #获取第0列的数据
values = sheet_obj.col_values(1,1) #获取第1列的数据
dict_data = {}
#组装成字典
for i in keys:
dict_data[i]=values[keys.index(i)]
#或者可以用zip方法,生成元组列表
zip_data= dict(zip(keys,values))
print(zip_data)
print("-----------------")
#打印所有行
for i in range(res5):
print(sheet_obj.row_values(i))
print("-------------------")
print(res)
print("-------------------")
print(res1)
print("-------------------")
print(res2)
print("-------------------")
print(res3)
print("-------------------")
print(res4)
print("-------------------")
print(res5)
运行结果:
E:\pyProject\pythonpytest\venv\Scripts\python.exe E:/pyProject/pythonpytest/testcase/demo.py
{'platformName': 'Android', 'platformVersion': '6.0.1', 'deviceName': '127.0.0.1:7555', 'appPackage': 'com.zhao.myreader', 'appActivity': 'com.zhao.myreader.ui.home.MainActivity', 'newCommandTimeout': 3600.0}
-----------------
['url', 'http://127.0.0.1:4723/wd/hub', 'a', 'b', 'c']
['platformName', 'Android', '', '', '']
['platformVersion', '6.0.1', '', '', '']
['deviceName', '127.0.0.1:7555', '', '', '']
['appPackage', 'com.zhao.myreader', '', '', '']
['appActivity', 'com.zhao.myreader.ui.home.MainActivity', '', '', '']
['newCommandTimeout', 3600.0, '', '', '']
-------------------
['url', 'http://127.0.0.1:4723/wd/hub', 'a', 'b', 'c']
-------------------
['http://127.0.0.1:4723/wd/hub', 'a']
-------------------
['http://127.0.0.1:4723/wd/hub', 'Android', '6.0.1', '127.0.0.1:7555', 'com.zhao.myreader', 'com.zhao.myreader.ui.home.MainActivity', 3600.0]
-------------------
['Android', '6.0.1']
-------------------
3600.0
-------------------
7
Process finished with exit code 0
excel:
url | http://127.0.0.1:4723/wd/hub | a | b | c |
platformName | Android | |||
platformVersion | 6.0.1 | |||
deviceName | 127.0.0.1:7555 | |||
appPackage | com.zhao.myreader | |||
appActivity | com.zhao.myreader.ui.home.MainActivity | |||
newCommandTimeout | 3600 |