zoukankan      html  css  js  c++  java
  • 接口自动化之 excel重构

    EXCEL基本用法
    import xlrd
    book = xlrd.open_workbook("文件地址")
    sh = book.sheet_by_index("表ID")
    print(sh.nrows)
    print(sh.cell_value(rowx= ,colx= ))


    重构后:

    import xlrd

    class OperationExcel:
    def __init__(self,filename=None,sheetid=None):
    #如果参数中文件名存在,则使用参数中的,不存在就使用默认的(主要要针对要读取的文件,如果读取其他位置的文件,就不使用else里的默认文件位置)
    if filename:
    self.filename = filename
    self.sheetid = sheetid

    else:
    self.filename = "默认要读取的文件位置(../dataconfig/interface.xlsx)"
    self.sheetid="默认sheetid(0或者1或者2....)"
    self.data = self.get_data(filename, sheetid)


    #获取sheet内容
    def get_data(self):
    book = xlrd.open_workbook(self.filename)
    sheet = book.sheet_by_index(self.sheetid)
    return sheet

    #获取sheet行数
    def get_lines(self):
    sh = self.data
    rows = sh.nrows
    print(rows)

    #获取单元格的内容
    def get_cell_value(self):
    sh = self.data
    value = sh.cell_value(rowx=1,colx=2)
    print(value)


    if __name__ == '__main__':
    opera = OperationExcel()
    print(opera.get_lines())
    print(opera.get_cell_value())
  • 相关阅读:
    .net core使用EasyNetQ做EventBus
    .NET Core DI 手动获取注入对象
    设置Jexus开机启动
    centos7开放关闭防火墙端口
    linux防火墙开放端口
    转 Jexus-5.6.3使用详解
    Mysql存储过程
    Reids笔记
    Lucene笔记
    Spring Junit 测试用例
  • 原文地址:https://www.cnblogs.com/lexus168/p/12556908.html
Copyright © 2011-2022 走看看