python接口自动化之openpyxl学习
由于接口自动化测试需要用到python的第三方库--openpyxl库,运用openpyxl库可以进行exl的读和写,再结合unittest测试框架,就能进行接口自动化测试。
一:openpyxl库的安装:pip install openpyxl
二:查看openpyxl库的属性
import openpyxl
dir1= dir(openpyxl)
print(dir1)
[‘DEFUSEDXML‘, ‘LXML‘, ‘NUMPY‘, ‘PANDAS‘, ‘Workbook‘, ‘__author__‘, ‘__author_email__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__license__‘, ‘__loader__‘, ‘__maintainer_email__‘, ‘__name__‘, ‘__package__‘, ‘__path__‘, ‘__spec__‘, ‘__url__‘, ‘__version__‘, ‘_constants‘, ‘cell‘, ‘chart‘, ‘chartsheet‘, ‘comments‘, ‘compat‘, ‘constants‘, ‘descriptors‘, ‘drawing‘, ‘formatting‘, ‘formula‘, ‘load_workbook‘, ‘packaging‘, ‘pivot‘, ‘reader‘, ‘styles‘, ‘utils‘, ‘workbook‘, ‘worksheet‘, ‘writer‘, ‘xml‘]
三:openpyxl库有两个常用的模块 workbook:创建excel工作簿 load_workbook:读写excel文件
1、查看workbook模块属性:
from openpyxl import workbook
print(dir(workbook))
包含如下属性值:
[‘Workbook‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__path__‘, ‘__spec__‘, ‘_writer‘, ‘absolute_import‘, ‘child‘, ‘defined_name‘, ‘external_link‘, ‘external_reference‘, ‘function_group‘, ‘properties‘, ‘protection‘, ‘smart_tags‘, ‘views‘, ‘web‘, ‘workbook‘]
2、查看workbook.Workbook的属性
from openpyxl import workbook
print(dir(workbook.Workbook))
包含如下属性值:
[‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__delitem__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getattribute__‘, ‘__getitem__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__iter__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__ne__‘, ‘__new__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__weakref__‘, ‘_add_sheet‘, ‘_data_only‘, ‘_read_only‘, ‘_setup_styles‘, ‘active‘, ‘add_named_range‘, ‘add_named_style‘, ‘chartsheets‘, ‘close‘, ‘copy_worksheet‘, ‘create_chartsheet‘, ‘create_named_range‘, ‘create_sheet‘, ‘data_only‘, ‘excel_base_date‘, ‘get_active_sheet‘, ‘get_index‘, ‘get_named_range‘, ‘get_named_ranges‘, ‘get_sheet_by_name‘, ‘get_sheet_names‘, ‘guess_types‘, ‘index‘, ‘mime_type‘, ‘named_styles‘, ‘path‘, ‘read_only‘, ‘remove‘, ‘remove_named_range‘, ‘remove_sheet‘, ‘save‘, ‘sheetnames‘, ‘style_names‘, ‘template‘, ‘worksheets‘, ‘write_only‘]
举例子:
#新建一个excel
Wb=workbook.Workbook() #创建一个excel工作薄
Wb.creat_sheet(“lbb”) #创建一个名为lbb的工作表
Wb.save(“py15_lbb.xlsx”) # 将工作薄另存为py15_lbb,可以指定特定路径
3、查看load_workbook模块属性:
from openpyxl import load_workbook
print(dir(load_workbook))
包含如下属性值:
[‘__annotations__‘, ‘__call__‘, ‘__class__‘, ‘__closure__‘, ‘__code__‘, ‘__defaults__‘, ‘__delattr__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__get__‘, ‘__getattribute__‘, ‘__globals__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__kwdefaults__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__name__‘, ‘__ne__‘, ‘__new__‘, ‘__qualname__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘]
举例子:
Excel的读写操作: 结论excel中读取到的数据类型:数字类型还是数字类型,其他类型读取到的都是字符串类型(eval函数转换成python可以识别的数据类型)
wb = load_workbook("py15_lbb.xlsx") #打开工作薄
sheet = wb["sheet1"] #定位到表单
value = sheet.cell(1,2) #读取第1行,第2列的值
sheet.cell(1,2).value = "lemon" # 更新第1行,第2列的值为lemon
row = sheet.max_row # 获取该sheet的最大行
column = sheet.max_column # 获取该sheet的最大列
原文地址:https://www.cnblogs.com/qiyuedetiankong/p/10886561.html