python+excel 实现从表格里读取用例,并把测试结果写在对应的列里

我们测试人员一般都把用例写在excel里,今天小编就把自己在工作中用到的知识点写出来,供大家参考和指点哈,有写的不对的,欢迎大家留言哈。。。。

在这里,我用的是openpyxl 中的load_workbook,导入就可以了,好了,废话不多说,直接上代码。。。

 

import xlrd,os
from openpyxl import load_workbook
from xlwt import *

class ReadExcel():

    def __init__(self,filename,sheetname):
        self.filename = filename
        self.sheetname = sheetname

    #从表格里读取数据
    def read_excel(self):
        workbook = xlrd.open_workbook(self.fileName)
        worksheet = workbook.sheet_by_name(self.sheetName)
        num_rows = worksheet.nrows
        cellList = []
        #获取所有的单元格内容
        for i in range(1,num_rows):
            cell_msg ={}
            cell_msg['case_id'] = worksheet.cell_value(i,0)
            cell_msg['header'] = worksheet.cell_value(i, 1)
            cell_msg['method'] = worksheet.cell_value(i, 2)
            cell_msg['url'] = worksheet.cell_value(i, 3)
            cell_msg['data'] = worksheet.cell_value(i, 4)
            cell_msg['ExpectedResult'] = worksheet.cell_value(i, 5)
            cellList.append(cell_msg)
        return cellList



    #往表格里写数据
    def write_excel(self,i,j,value):
        if not os.path.exists(self.filename):
            wb = Workbook()
            sh = wb.create_sheet(self.sheetname)
        else:
            wb = load_workbook(self.filename)
            sheet = wb[self.sheetname]

            sheet.cell(i,j).value = value
            wb.save(self.filename)
            return wb

if __name__ =='__main__':
    a = ReadExcel('excel的路径', '表里的指定的sheet名')
    a.write_excel(行,列,'aaaaa')
    print('添加成功')

 

你可能感兴趣的:(python+excel 实现从表格里读取用例,并把测试结果写在对应的列里)