python自动化运维Python接口测试Python自动化测试

9 Python xlrd

2019-08-14  本文已影响0人  降水

xlrd
python操作excel主要用到xlrd和xlwt两个模块,即xlrd是读excel,xlwt是写excel的库。

关键字:


1 xlrd安装
pip install  xlrd
2 xlrd官方文档

https://xlrd.readthedocs.io/en/latest/api.html

3 xlrd语法 - 文档读取【workbook 】
import xlrd
workbook = xlrd.open_workbook("/python-test/excel/test.xlsx")

4 xlrd语法 - 文档读取【sheet, table】
# 返回一个xlrd.sheet.Sheet()对象
table = workbook.sheet_by_name("工作表1") #通过名称获取
table = workbook.sheets()[0]             #通过索引顺序获取
table = workbook.sheet_by_index(1)       #通过索引顺序获取    
#
names = workbook.sheet_names()     # 返回book中所有工作表的名字
workbook.sheet_loaded("name")      # 检查某个sheet是否导入完毕sheet_name or index
5 xlrd语法 - 行操作【row】
num = table.nrows             #获取该sheet中的有效行数
data = table.row(rowx)        #返回由该行中所有的单元格对象组成的列表
data = table.row_slice(rowx)  #返回由该行中所有的单元格对象组成的列表
data = table.row_types(rowx, start_colx=0, end_colx=None) #返回由该行中所有单元格的数据类型组成的列表
table.row_values(rowx, start_colx=0, end_colx=None)#返回由该行中所有单元格的数据组成的列表
table.row_len(rowx)                                #返回该列的有效单元格长度
6 xlrd语法 - 列操作【col】
ncols = table.ncols   #获取列表的有效列数
table.col(colx, start_rowx=0, end_rowx=None)        #返回由该列中所有的单元格对象组成的列表
table.col_slice(colx, start_rowx=0, end_rowx=None)  #返回由该列中所有的单元格对象组成的列表
table.col_types(colx, start_rowx=0, end_rowx=None)  #返回由该列中所有单元格的数据类型组成的列表
table.col_values(colx, start_rowx=0, end_rowx=None) #返回由该列中所有单元格的数据组成的列表
7 xlrd语法 - 单元格操作【cell】
value = table.cell_value(rowx,colx)     #返回单元格中的数据
table.cell(rowx,colx)           #返回单元格对象
table.cell_type(rowx,colx)      #返回单元格中的数据类型
table.cell_xf_index(rowx, colx) #暂时还没有搞懂
8 xlrd 实例
import unittest, xlrd, requests
class wxhash(unittest.TestCase):
  def test_tries(self):
     workbook = xlrd.open_workbook("./python-test/excel/test1.xlsx")
     table = workbook.sheet_by_name("test")
     xlsx_num = table.nrows
     for i in range(1, xlsx_num):
       test_id   = table.cell_value(i, 0)
       test_name = table.cell_value(i, 1)
       url       = table.cell_value(i, 2)
       method    = table.cell_value(i, 3)
       data      = table.cell_value(i, 4)
       code      = table.cell_value(i, 5)

       if method == "get" :
         response = requests.get(url + "?" + data)
         res = response.json()
         self.assertEqual(res["code"], code)
报告结果截图如下

-- Github 地址 https://github.com/mingyuanHub/python-game-test

image.png
上一篇 下一篇

猜你喜欢

热点阅读