• 接口自动化测试Python(2)_使用python对excel进行操作


    如何使用Python对excel进行简单的操作

    一. 通过pip安装xlwt, xlrd这两个模块

          *pip install xlwt

          *pip install xlrd   


    二. 准备好一份接口测试用例,excel文档

    三. python脚本读取excel内容

     # 读取excel内容
    import xlrd

    # 获取excel文件存放地址(加r声明为raw字符串,这样就不会处理其中的转义了。否则,可能会报错)
    data = xlrd.open_workbook(r'..dataconfiginterfaceTestCase.xlsx')
    # 获取第1sheet
    tables = data.sheets()[0]
    # 打印sheet名字
    print data.sheet_names()
    # 打印行数
    print tables.nrows
    # 打印第3行第4
    print tables.cell_value(2, 3)

    运行结果如下:

     

    四. 进阶-对象实例化

    # coding=utf-8
    # 读取excel内容
    import xlrd


    class OperationExcel:
    def __init__(self, file_name=None, sheet_id=None):
    if file_name:
    self.file_name = self.file_name
    self.sheet_id = self.sheet_id

    else:
    self.file_name = '..dataconfiginterfaceTestCase.xlsx'
    self.sheet_id = 0
    self.data = self.get_data()

    # 获取sheets的内容
    def get_data(self):
    data = xlrd.open_workbook(self.file_name)
    tables = data.sheets()[self.sheet_id]
    return tables

    # 获取单元格的行数
    def get_lines(self):
    tables = self.data
    return tables.nrows

    # 获取某一个单元格的内容
    def get_cell_value(self, row, col):
    return self.data.cell_value(row, col)


    if __name__ == '__main__':
    opers = OperationExcel()
    print opers.get_data().nrows
    print opers.get_lines()
    print opers.get_cell_value(1, 3)

  • 相关阅读:
    三种负载均衡 Nginx、Dubbo、Ribbon 区别
    Docker基础学习
    主从复制报错2061:Authentication plugin 'caching_sha2_password' reported error:Authentication require secure connection
    LRU、LFU算法区别
    CAP理论原理
    Mysql安装服务到Window服务列表
    从零开始掌握 HAProxy 负载均衡器,详细!!
    一举拿下Nginx
    Nginx 负载均衡配置误区
    Linux自动化技巧
  • 原文地址:https://www.cnblogs.com/cocowang68/p/7903329.html
Copyright © 2020-2023  润新知