test_project_excel 練習(xí)目錄:
import xlrd
class ts_excel():
def _open_excel(self, path):
try:
work_book = xlrd.open_workbook(path, 'rb')
except Exception as e:
print(e)
raise Exception("can not open work book! Pls check it at path:%s" % path)
return work_book
def _open_sheet_by_index(self, work_book, index):
try:
sheet = work_book.sheet_by_index(index)
except:
raise Exception("Can not open sheet by index %s" % index)
return sheet
def _open_sheet_by_name(self, work_book, sheet_name):
sheet = None
for st_name in work_book.sheet_names():
if str(st_name) == sheet_name:
sheet = work_book.sheet_by_name(sheet_name)
if sheet == None:
raise Exception("Can not open sheet by name %s" % sheet_name)
return sheet
def _get_data_from_sheet(self, sheet, col_index=2):
nrows = sheet.nrows
print(nrows)
ncols = sheet.ncols
if ncols < col_index:
raise Exception("Can not find %d cols from sheet %s" % (col_index, sheet.name))
# colnames = sheet.row_value(0) #第一行的數(shù)據(jù)
list1 = []
list2 = []
for rownum in range(1, nrows):
row_col0 = sheet.row(rownum)[0].value
row_col1 = sheet.row(rownum)[1].value
list1.append(row_col0)
list2.append(row_col1)
dict_date = dict(zip(list1, list2))
print(dict_date)
return dict_date
def _get_testdata_from_sheet(self, sheet, col_index=3):
nrows = sheet.nrows
print(nrows)
ncols = sheet.ncols
if ncols < col_index:
raise Exception("Can not find %dth cols from sheet %s" % (col_index, sheet.name))
list1 = []
list2 = []
for rownum in nrows:
for colnum in ncols:
list1.append(sheet.row(rownum)[colnum].value)
list2.append(list1)
list1 = []
print(list2)
return list2
def get_testdata_from_excel_by_index(self, path, index):
work_book = self._open_excel(path)
sheet = self._open_sheet_by_index(work_book, index)
dict_data = self._get_data_from_sheet(sheet)
return dict_data
def get_testdata_from_excel_by_name(self, path, sheet_name=u'Sheet1'):
work_book = self._open_excel(path)
sheet = self._open_sheet_by_name(work_book, sheet_name)
dict_data = self._get_data_from_sheet(sheet)
return dict_data
def get_testdata_from_excel(self, path, sheet_name=u'Sheet1'):
work_book = self._open_excel(path)
sheet = self._open_sheet_by_name(work_book, sheet_name)
list_data = self._get_data_from_sheet(sheet)
return list_data
#MyKey().get_testdata_from_excel_by_index('F:\python學(xué)習(xí)\\test_project_excel\\test_excel01.xlsx', 0)
#MyKey().get_testdata_from_excel_by_name('F:\python學(xué)習(xí)\\test_project_excel\\test_excel01.xlsx', 'account')
#MyKey().get_testdata_from_excel('F:\python學(xué)習(xí)\\test_project_excel\\test_excel01.xlsx', 'account')
-
導(dǎo)入自定義庫
- 創(chuàng)建測試
- 新建項目 : test_project_excel
- 新建測試集 : t1.robot
- 新建測試:
test_case02
test_case03
test_case04 - 導(dǎo)入?yún)?shù)
*** Settings ***
Library MyLibrary
Library MyKey/ts_excel.py
*** Variables ***
${path} ./test_excel01.xlsx
${index} ${0}
*** Test Cases ***
test_demo01
test_a_b 1 2
test_case02
${data} get_testdata_from_excel_by_index ${path} ${index}
test_case03
${data2} get_testdata_from_excel_by_name ${path} account
test_case04
${data3} get_testdata_from_excel ${path} account
-
運行結(jié)果
命令運行結(jié)果