getExcelData.py 2 KB
Newer Older
taoke committed
1 2
import xlrd,re,json

taoke committed
3
def get_excelData(workBook,sheetName,caseName):
taoke committed
4 5
    """
    :param sheetName: sheet表名
taoke committed
6
    :param caseName:  从excle中第一列关键字(字母)
7
    :return: # [{},{},{}]
taoke committed
8 9
    """
    workSheet = workBook.sheet_by_name(sheetName)
10
    list_title = workSheet.row_values(0)
taoke committed
11
    try:
12 13 14 15 16 17
        num_url = list_title.index('url')
        num_headers = list_title.index('headers')
        num_method = list_title.index('method')
        num_reqData = list_title.index('reqData')
        num_expectData = list_title.index('expectData')
        lis = []
taoke committed
18

19 20 21 22 23
        idx = 0
        try:
            for one in workSheet.col_values(0):
                result = ''.join(re.findall(r'[A-Za-z]', one))  # 抽取字母字符串
                if caseName == result:
taoke committed
24
                    dict0 = {"url": "", "headers": "", "method": "", "reqData": "", "expectData": ""}
25 26 27
                    dict0['url'] = workSheet.cell_value(idx, num_url)
                    dict0['headers'] = workSheet.cell_value(idx, num_headers)
                    dict0['method'] = workSheet.cell_value(idx, num_method)
taoke committed
28 29
                    dict0['reqData'] = workSheet.cell_value(idx, num_reqData)
                    dict0['expectData'] = workSheet.cell_value(idx, num_expectData)
30 31 32 33

                    # json字符串转换成字典
                    dict0['reqData'] = json.loads(dict0['reqData'])
                    dict0['expectData'] = json.loads(dict0['expectData'])
taoke committed
34 35 36 37
                    try:
                        dict0['headers'] = json.loads(dict0['headers'])
                    except:
                        print('header无')
38 39 40 41 42
                    lis.append(dict0)
                idx += 1
            return lis
        except:
            print("excle中header值或参数或期望不是json字符串")
taoke committed
43
    except:
44 45
        print("检查excle中标题是否正确")

taoke committed
46
if __name__ == '__main__':
taoke committed
47
    workBook = xlrd.open_workbook('../../test_case_data/bmy/bmy_case.xlsx')
taoke committed
48 49 50 51
    li = get_excelData(workBook,"登录模块","Login")
    print(li)