python自動(dòng)化測試之如何解析excel文件
前言
自動(dòng)化測試中我們存放數(shù)據(jù)無非是使用文件或者數(shù)據(jù)庫,那么文件可以是csv,xlsx,xml,甚至是txt文件,通常excel文件往往是我們的首選,無論是編寫測試用例還是存放測試數(shù)據(jù),excel都是很方便的。那么今天我們就把不同模塊處理excel文件的方法做個(gè)總結(jié),直接做封裝,方便我們以后直接使用,增加工作效率。
openpyxl
openpyxl是個(gè)第三方庫,首先我們使用命令 pip install openpyxl 直接安裝
注:openpyxl操作excel時(shí),行號和列號都是從1開始計(jì)算的
封裝代碼
"""
------------------------------------
@Time : 2019/5/13 18:00
@Auth : linux超
@File : ParseExcel.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
------------------------------------
"""
from openpyxl import load_workbook
from openpyxl.styles import Font
from openpyxl.styles.colors import BLACK
from collections import namedtuple
class ParseExcel(object):
"""解析excel文件"""
def __init__(self, filename, sheet_name=None):
try:
self.filename = filename
self.sheet_name = sheet_name
self.wb = load_workbook(self.filename)
if self.sheet_name is None:
self.work_sheet = self.wb.active
else:
self.work_sheet = self.wb[self.sheet_name]
except FileNotFoundError as e:
raise e
def get_max_row_num(self):
"""獲取最大行號"""
max_row_num = self.work_sheet.max_row
return max_row_num
def get_max_column_num(self):
"""獲取最大列號"""
max_column = self.work_sheet.max_column
return max_column
def get_cell_value(self, coordinate=None, row=None, column=None):
"""獲取指定單元格的數(shù)據(jù)"""
if coordinate is not None:
try:
return self.work_sheet[coordinate].value
except Exception as e:
raise e
elif coordinate is None and row is not None and column is not None:
if isinstance(row, int) and isinstance(column, int):
return self.work_sheet.cell(row=row, column=column).value
else:
raise TypeError('row and column must be type int')
else:
raise Exception("Insufficient Coordinate of cell!")
def get_row_value(self, row):
"""獲取某一行的數(shù)據(jù)"""
column_num = self.get_max_column_num()
row_value = []
if isinstance(row, int):
for column in range(1, column_num + 1):
values_row = self.work_sheet.cell(row, column).value
row_value.append(values_row)
return row_value
else:
raise TypeError('row must be type int')
def get_column_value(self, column):
"""獲取某一列數(shù)據(jù)"""
row_num = self.get_max_column_num()
column_value = []
if isinstance(column, int):
for row in range(1, row_num + 1):
values_column = self.work_sheet.cell(row, column).value
column_value.append(values_column)
return column_value
else:
raise TypeError('column must be type int')
def get_all_value_1(self):
"""獲取指定表單的所有數(shù)據(jù)(除去表頭)"""
max_row_num = self.get_max_row_num()
max_column = self.get_max_column_num()
values = []
for row in range(2, max_row_num + 1):
value_list = []
for column in range(1, max_column + 1):
value = self.work_sheet.cell(row, column).value
value_list.append(value)
values.append(value_list)
return values
def get_all_value_2(self):
"""獲取指定表單的所有數(shù)據(jù)(除去表頭)"""
rows_obj = self.work_sheet.iter_rows(min_row=2, max_row=self.work_sheet.max_row,
values_only=True) # 指定values_only 會(huì)直接提取數(shù)據(jù)不需要再使用cell().value
values = []
for row_tuple in rows_obj:
value_list = []
for value in row_tuple:
value_list.append(value)
values.append(value_list)
return values
def get_excel_title(self):
"""獲取sheet表頭"""
title_key = tuple(self.work_sheet.iter_rows(max_row=1, values_only=True))[0]
return title_key
def get_listdict_all_value(self):
"""獲取所有數(shù)據(jù),返回嵌套字典的列表"""
sheet_title = self.get_excel_title()
all_values = self.get_all_value_2()
value_list = []
for value in all_values:
value_list.append(dict(zip(sheet_title, value)))
return value_list
def get_list_nametuple_all_value(self):
"""獲取所有數(shù)據(jù),返回嵌套命名元組的列表"""
sheet_title = self.get_excel_title()
values = self.get_all_value_2()
excel = namedtuple('excel', sheet_title)
value_list = []
for value in values:
e = excel(*value)
value_list.append(e)
return value_list
def write_cell(self, row, column, value=None, bold=True, color=BLACK):
"""
指定單元格寫入數(shù)據(jù)
:param work_sheet:
:param row: 行號
:param column: 列號
:param value: 待寫入數(shù)據(jù)
:param bold: 加粗, 默認(rèn)加粗
:param color: 字體顏色,默認(rèn)黑色
:return:
"""
try:
if isinstance(row, int) and isinstance(column, int):
cell_obj = self.work_sheet.cell(row, column)
cell_obj.font = Font(color=color, bold=bold)
cell_obj.value = value
self.wb.save(self.filename)
else:
raise TypeError('row and column must be type int')
except Exception as e:
raise e
if __name__ == '__main__':
pe = ParseExcel('testdata.xlsx')
# sheet = pe.get_sheet_object('testcase')
column_row = pe.get_max_column_num()
print('最大列號:', column_row)
max_row = pe.get_max_row_num()
print('最大行號:', max_row)
#
cell_value_1 = pe.get_cell_value(row=2, column=3)
print('第%d行, 第%d列的數(shù)據(jù)為: %s' % (2, 3, cell_value_1))
cell_value_2 = pe.get_cell_value(coordinate='A5')
print('A5單元格的數(shù)據(jù)為: {}'.format(cell_value_2))
value_row = pe.get_row_value(3)
print('第{}行的數(shù)據(jù)為:{}'.format(3, value_row))
value_column = pe.get_column_value(2)
print('第{}列的數(shù)據(jù)為:{}'.format(2, value_column))
#
values_1 = pe.get_all_value_1()
print('第一種方式獲取所有數(shù)據(jù)\n', values_1)
values_2 = pe.get_all_value_2()
print('第二種方式獲取所有數(shù)據(jù)\n', values_2)
title = pe.get_excel_title()
print('表頭為\n{}'.format(title))
dict_value = pe.get_listdict_all_value()
print('所有數(shù)據(jù)組成的嵌套字典的列表:\n', dict_value)
#
namedtuple_value = pe.get_list_nametuple_all_value()
print('所有數(shù)據(jù)組成的嵌套命名元組的列表:\n', namedtuple_value)
pe.write_cell(1, 2, 'Tc_title')
# add by linux超 at 2019/05/22 15:58
上面這個(gè)封裝如如果用來同時(shí)操作同一個(gè)excel文件的兩個(gè)sheet寫入數(shù)據(jù)時(shí),會(huì)有點(diǎn)小bug(寫完后你會(huì)發(fā)現(xiàn)兩個(gè)表單有一個(gè)是沒有數(shù)據(jù)的)
其實(shí)原因很簡單:不同對象擁有自己獨(dú)立的屬性, 當(dāng)你寫操作的時(shí)候其實(shí)每個(gè)對象只針對自己的表單做了保存,所以最后一個(gè)對象寫完數(shù)據(jù)后,只保存了自己的表單,其他的對象的表單實(shí)際是沒有保存的。針對這個(gè)問題,對上面封裝的代碼進(jìn)行了輕微改動(dòng)
"""
------------------------------------
@Time : 2019/5/22 9:11
@Auth : linux超
@File : ParseExcel.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
------------------------------------
"""
from openpyxl import load_workbook
from openpyxl.styles import Font
from openpyxl.styles.colors import BLACK
from collections import namedtuple
class ParseExcel(object):
"""解析excel文件"""
def __init__(self, filename):
try:
self.filename = filename
self.__wb = load_workbook(self.filename)
except FileNotFoundError as e:
raise e
def get_max_row_num(self, sheet_name):
"""獲取最大行號"""
max_row_num = self.__wb[sheet_name].max_row
return max_row_num
def get_max_column_num(self, sheet_name):
"""獲取最大列號"""
max_column = self.__wb[sheet_name].max_column
return max_column
def get_cell_value(self, sheet_name, coordinate=None, row=None, column=None):
"""獲取指定單元格的數(shù)據(jù)"""
if coordinate is not None:
try:
return self.__wb[sheet_name][coordinate].value
except Exception as e:
raise e
elif coordinate is None and row is not None and column is not None:
if isinstance(row, int) and isinstance(column, int):
return self.__wb[sheet_name].cell(row=row, column=column).value
else:
raise TypeError('row and column must be type int')
else:
raise Exception("Insufficient Coordinate of cell!")
def get_row_value(self, sheet_name, row):
"""獲取某一行的數(shù)據(jù)"""
column_num = self.get_max_column_num(sheet_name)
row_value = []
if isinstance(row, int):
for column in range(1, column_num + 1):
values_row = self.__wb[sheet_name].cell(row, column).value
row_value.append(values_row)
return row_value
else:
raise TypeError('row must be type int')
def get_column_value(self, sheet_name, column):
"""獲取某一列數(shù)據(jù)"""
row_num = self.get_max_column_num(sheet_name)
column_value = []
if isinstance(column, int):
for row in range(1, row_num + 1):
values_column = self.__wb[sheet_name].cell(row, column).value
column_value.append(values_column)
return column_value
else:
raise TypeError('column must be type int')
def get_all_value_1(self, sheet_name):
"""獲取指定表單的所有數(shù)據(jù)(除去表頭)"""
max_row_num = self.get_max_row_num(sheet_name)
max_column = self.get_max_column_num(sheet_name)
values = []
for row in range(2, max_row_num + 1):
value_list = []
for column in range(1, max_column + 1):
value = self.__wb[sheet_name].cell(row, column).value
value_list.append(value)
values.append(value_list)
return values
def get_all_value_2(self, sheet_name):
"""獲取指定表單的所有數(shù)據(jù)(除去表頭)"""
rows_obj = self.__wb[sheet_name].iter_rows(min_row=2, max_row=self.__wb[sheet_name].max_row, values_only=True)
values = []
for row_tuple in rows_obj:
value_list = []
for value in row_tuple:
value_list.append(value)
values.append(value_list)
return values
def get_excel_title(self, sheet_name):
"""獲取sheet表頭"""
title_key = tuple(self.__wb[sheet_name].iter_rows(max_row=1, values_only=True))[0]
return title_key
def get_listdict_all_value(self, sheet_name):
"""獲取所有數(shù)據(jù),返回嵌套字典的列表"""
sheet_title = self.get_excel_title(sheet_name)
all_values = self.get_all_value_2(sheet_name)
value_list = []
for value in all_values:
value_list.append(dict(zip(sheet_title, value)))
return value_list
def get_list_nametuple_all_value(self, sheet_name):
"""獲取所有數(shù)據(jù),返回嵌套命名元組的列表"""
sheet_title = self.get_excel_title(sheet_name)
values = self.get_all_value_2(sheet_name)
excel = namedtuple('excel', sheet_title)
value_list = []
for value in values:
e = excel(*value)
value_list.append(e)
return value_list
def write_cell(self, sheet_name, row, column, value=None, bold=True, color=BLACK):
if isinstance(row, int) and isinstance(column, int):
try:
cell_obj = self.__wb[sheet_name].cell(row, column)
cell_obj.font = Font(color=color, bold=bold)
cell_obj.value = value
self.__wb.save(self.filename)
except Exception as e:
raise e
else:
raise TypeError('row and column must be type int')
if __name__ == '__main__':
pe = ParseExcel('testdata.xlsx')
print(pe.get_all_value_2('division'))
print(pe.get_list_nametuple_all_value('division'))
column_row = pe.get_max_column_num('division')
print('最大列號:', column_row)
max_row = pe.get_max_row_num('division')
print('最大行號:', max_row)
cell_value_1 = pe.get_cell_value('division', row=2, column=3)
print('第%d行, 第%d列的數(shù)據(jù)為: %s' % (2, 3, cell_value_1))
cell_value_2 = pe.get_cell_value('division', coordinate='A5')
print('A5單元格的數(shù)據(jù)為: {}'.format(cell_value_2))
value_row = pe.get_row_value('division', 3)
print('第{}行的數(shù)據(jù)為:{}'.format(3, value_row))
value_column = pe.get_column_value('division', 2)
print('第{}列的數(shù)據(jù)為:{}'.format(2, value_column))
values_1 = pe.get_all_value_1('division')
print('第一種方式獲取所有數(shù)據(jù)\n', values_1)
values_2 = pe.get_all_value_2('division')
print('第二種方式獲取所有數(shù)據(jù)\n', values_2)
title = pe.get_excel_title('division')
print('表頭為\n{}'.format(title))
dict_value = pe.get_listdict_all_value('division')
print('所有數(shù)據(jù)組成的嵌套字典的列表:\n', dict_value)
namedtuple_value = pe.get_list_nametuple_all_value('division')
print('所有數(shù)據(jù)組成的嵌套命名元組的列表:\n', namedtuple_value)
pe.write_cell('division', 1, 2, 'Tc_title')
xlrd
安裝xlrd,此模塊只支持讀操作, 如果要寫需要使用xlwt或者使用xlutils配合xlrd, 但是使用xlwt只能對新的excel文件進(jìn)行寫操作,無法對原有文件進(jìn)行寫, 所以這里選擇是用xlutils
但是還有一個(gè)問題就是,如果使用xlutils, 那么我們的excel文件需要以.xls 為后綴。因?yàn)橐詘lsx為后綴無法實(shí)現(xiàn)寫,會(huì)報(bào)錯(cuò)(親測,因?yàn)閒ormatting_info參數(shù)還沒有對新版本的xlsx的格式完成兼容)
注:xlrd操作excel時(shí),行號和列號都是從0開始計(jì)算的
封裝代碼
"""
------------------------------------
@Time : 2019/5/13 21:22
@Auth : linux超
@File : ParseExcel_xlrd.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
------------------------------------
"""
import xlrd
from xlutils import copy
from collections import namedtuple
class ParseExcel(object):
# xlrd 解析excel, 行號和列號都是從0開始的
def __init__(self, filename, sheet):
try:
self.filename = filename
self.sheet = sheet
self.wb = xlrd.open_workbook(self.filename, formatting_info=True)
if isinstance(sheet, str):
self.sheet = self.wb.sheet_by_name(sheet)
elif isinstance(sheet, int):
self.sheet = self.wb.sheet_by_index(sheet)
else:
raise TypeError('sheet must be int or str')
except Exception as e:
raise e
def get_max_row(self):
"""獲取表單的最大行號"""
max_row_num = self.sheet.nrows
return max_row_num
def get_max_column(self):
"""獲取表單的最大列號"""
min_row_num = self.sheet.ncols
return min_row_num
def get_cell_value(self, row, column):
"""獲取某個(gè)單元格的數(shù)據(jù)"""
if isinstance(row, int) and isinstance(column, int):
values = self.sheet.cell(row-1, column-1).value
return values
else:
raise TypeError('row and column must be type int')
def get_row_values(self, row):
"""獲取某一行的數(shù)據(jù)"""
if isinstance(row, int):
values = self.sheet.row_values(row-1)
return values
else:
raise TypeError('row must be type int')
def get_column_values(self, column):
"""獲取某一列的數(shù)據(jù)"""
if isinstance(column, int):
values = self.sheet.col_values(column-1)
return values
else:
raise TypeError('column must be type int')
def get_table_title(self):
"""獲取表頭"""
table_title = self.get_row_values(1)
return table_title
def get_all_values_dict(self):
"""獲取所有的數(shù)據(jù),不包括表頭,返回一個(gè)嵌套字典的列表"""
max_row = self.get_max_row()
table_title = self.get_table_title()
value_list = []
for row in range(2, max_row):
values = self.get_row_values(row)
value_list.append(dict(zip(table_title, values)))
return value_list
def get_all_values_nametuple(self):
"""獲取所有的數(shù)據(jù),不包括表頭,返回一個(gè)嵌套命名元組的列表"""
table_title = self.get_table_title()
max_row = self.get_max_row()
excel = namedtuple('excel', table_title)
value_list = []
for row in range(2, max_row):
values = self.get_row_values(row)
e = excel(*values)
value_list.append(e)
return value_list
def write_value(self, sheet_index, row, column, value):
"""寫入某個(gè)單元格數(shù)據(jù)"""
if isinstance(row, int) and isinstance(column, int):
if isinstance(sheet_index, int):
wb = copy.copy(self.wb)
worksheet = wb.get_sheet(sheet_index)
worksheet.write(row-1, column-1, value)
wb.save(self.filename)
else:
raise TypeError('{} must be int'.format(sheet_index))
else:
raise TypeError('{} and {} must be int'.format(row, column))
if __name__ == '__main__':
pe = ParseExcel('testdata.xls', 'testcase')
print('最大行號:', pe.get_max_row())
print('最大列號:', pe.get_max_column())
print('第2行第3列數(shù)據(jù):', pe.get_cell_value(2, 3))
print('第2行數(shù)據(jù)', pe.get_row_values(2))
print('第3列數(shù)據(jù)', pe.get_column_values(3))
print('表頭:', pe.get_table_title())
print('所有的數(shù)據(jù)返回嵌套字典的列表:', pe.get_all_values_dict())
print('所有的數(shù)據(jù)返回嵌套命名元組的列表:', pe.get_all_values_nametuple())
pe.write_value(0, 1, 3, 'test')
pandas
pandas是一個(gè)做數(shù)據(jù)分析的庫, 總是感覺在自動(dòng)化測試中使用pandas解析excel文件讀取數(shù)據(jù)有點(diǎn)大材小用,不論怎樣吧,還是把pandas解析excel文件寫一下把
我這里只封裝了讀,寫的話我這有點(diǎn)小問題,后面改好再追加代碼吧。
請先pip install pandas安裝pandas
封裝代碼
"""
------------------------------------
@Time : 2019/5/13 14:00
@Auth : linux超
@File : ParseExcel_pandas.py
@IDE : PyCharm
@Motto: Real warriors,dare to face the bleak warning,dare to face the incisive error!
------------------------------------
"""
import pandas as pd
class ParseExcel(object):
def __init__(self, filename, sheet_name=None):
try:
self.filename = filename
self.sheet_name = sheet_name
self.df = pd.read_excel(self.filename, self.sheet_name)
except Exception as e:
raise e
def get_row_num(self):
"""獲取行號組成的列表, 從0開始的"""
row_num_list = self.df.index.values
return row_num_list
def get_cell_value(self, row, column):
"""獲取某一個(gè)單元格的數(shù)據(jù)"""
try:
if isinstance(row, int) and isinstance(column, int):
cell_value = self.df.ix[row-2, column-1] # ix的行參數(shù)是按照有效數(shù)據(jù)行,且從0開始
return cell_value
else:
raise TypeError('row and column must be type int')
except Exception as e:
raise e
def get_table_title(self):
"""獲取表頭, 返回列表"""
table_title = self.df.columns.values
return table_title
def get_row_value(self, row):
"""獲取某一行的數(shù)據(jù), 行號從1開始"""
try:
if isinstance(row, int):
row_data = self.df.ix[row-2].values
return row_data
else:
raise TypeError('row must be type int')
except Exception as e:
raise e
def get_column_value(self, col_name):
"""獲取某一列數(shù)據(jù)"""
try:
if isinstance(col_name, str):
col_data = self.df[col_name].values
return col_data
else:
raise TypeError('col_name must be type str')
except Exception as e:
raise e
def get_all_value(self):
"""獲取所有的數(shù)據(jù),不包括表頭, 返回嵌套字典的列表"""
rows_num = self.get_row_num()
table_title = self.get_table_title()
values_list = []
for i in rows_num:
row_data = self.df.ix[i, table_title].to_dict()
values_list.append(row_data)
return values_list
if __name__ == '__main__':
pe = ParseExcel('testdata.xlsx', 'testcase')
print(pe.get_row_num())
print(pe.get_table_title())
print(pe.get_all_value())
print(pe.get_row_value(2))
print(pe.get_cell_value(2, 3))
print(pe.get_column_value('Tc_title'))
總結(jié)
使用了3種方法,4個(gè)庫 xlrd,openpyxl,xlwt,pandas 操作excel文件,個(gè)人感覺還是使用openpyxl比較適合在自動(dòng)化中使用,當(dāng)然不同人有不同選擇,用哪個(gè)區(qū)別也不是很大。
以上3種方法,都可以拿來直接使用,不需要再做封裝了 !
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
手把手帶你打造一個(gè)Pytest數(shù)據(jù)分離測試框架
數(shù)據(jù)分離測試框架是一種測試框架設(shè)計(jì)模式,旨在將測試數(shù)據(jù)與測試邏輯分離,以提高測試用例的可維護(hù)性、可讀性和復(fù)用性,本文就來實(shí)現(xiàn)一下,感興趣的可以了解一下2024-03-03
Python 相對路徑報(bào)錯(cuò):"No such file or 
如果你取相對路徑不是在主文件里,可能就會(huì)有相對路徑問題:"No such file or directory",由于python 的相對路徑,相對的都是主文件所以會(huì)出現(xiàn)Python 相對路徑報(bào)錯(cuò),今天小編給大家?guī)砹送昝澜鉀Q方案,感興趣的朋友一起看看吧2023-02-02
Python跨文件調(diào)用函數(shù)以及在一個(gè)文件中執(zhí)行另一個(gè)文件
這篇文章主要給大家介紹了關(guān)于Python跨文件調(diào)用函數(shù)以及在一個(gè)文件中執(zhí)行另一個(gè)文件的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-12-12
python Matplotlib畫圖之調(diào)整字體大小的示例
本篇文章主要介紹了python Matplotlib畫圖之調(diào)整字體大小的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
Python3使用騰訊云文字識別(騰訊OCR)提取圖片中的文字內(nèi)容實(shí)例詳解
這篇文章主要介紹了Python3使用騰訊云文字識別(騰訊OCR)提取圖片中的文字內(nèi)容方法詳解,需要的朋友可以參考下2020-02-02
Python中函數(shù)的參數(shù)定義和可變參數(shù)用法實(shí)例分析
這篇文章主要介紹了Python中函數(shù)的參數(shù)定義和可變參數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python中參數(shù)定義與可變參數(shù)的具體使用方法,需要的朋友可以參考下2015-06-06
Python實(shí)現(xiàn)針對給定字符串尋找最長非重復(fù)子串的方法
這篇文章主要介紹了Python實(shí)現(xiàn)針對給定字符串尋找最長非重復(fù)子串的方法,涉及Python針對字符串的遍歷、排序、計(jì)算等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04

