-- coding: utf-8 --
“”"
@Time : 2019/7/22 9:15
@Auth : 潇潇
@File : excel_frame.py
@IDE : PyCharm
@Motto: your story may not have such a happy beginning ,
but that doesn’t make who you are .
It is the rest of your story,
who you choose to be
“”"
import openpyxl
class HandleExcel:
‘’’
操作excel类
‘’’
def init(self, filename, sheetname=None):
self.filename = filename
self.sheetname = sheetname
def get_excel(self):
"""
获取excel中的用例
:return:
"""
wb = openpyxl.load_workbook(self.filename)
if self.sheetname is None:
ws = wb.active
else:
ws = wb[self.sheetname]
head_tuple = tuple(ws.iter_rows(max_row=1, values_only=True))[0]
one_list = []
for other_tuple in tuple(ws.iter_rows(min_row=2, values_only=True)):
one_list.append(dict(zip(head_tuple, other_tuple)))
return one_list
def write_excel(self, row, actual_col, actual, result_col, result):
"""
写入excel文件
:param row:要写入用例的行数
:param actual_col:要写入实际值得列数
:param actual:要写入的实际值
:param reslut_col:要写入结果得列数
:param result:要写入的结果
:return:
"""
wb = openpyxl.load_workbook(self.filename)
if self.sheetname is None:
ws = wb.active
else:
ws = wb[self.sheetname]
if isinstance(row, int) and (2 <= row <= ws.max_row):
ws.cell(row=row, column=actual_col, value=actual)
ws.cell(row=row, column=result_col, value=result)
wb.save(self.filename)
wb.close()
else:
print("传入的行号有误")
if name == ‘main’:
do_excel = HandleExcel(‘cases.xlsx’)
do_excel.get_excel()
do_excel.write_excel(2, 10, 10)