excel文件的封装处理-Handle_excle

读取的excel文件、文件扩展名:xlsx

case_id title expected param result
1 登录成功 成功
2 登录失败 成功
import openpyxl

class Handle_excle():
	def __init__(self,file_name,sheet_name):
		self.file_name = file_name
		self.sheet_name = sheet_name

	def read(self):
		workbook = openpyxl.load_workbook(self.file_name)
		sh = workbook[self.sheet_name]
		res = list(sh.rows)
		# 获取excel第一行数据
		title = [i.value for i in res[0]]
		case_list = []
		# 变量第一行以外的数据
		for item in res[1:]:
			data = [i.value for i in item]
			dic = dict(zip(title, data))
			case_list.append(dic)
		return case_list

	def write(self,row,column,value):
		workbook = openpyxl.load_workbook(self.file_name)
		sh = workbook[self.sheet_name]
		sh.cell(row,column,value)
		workbook.save(self.file_name)


if __name__ == '__main__':
	db = Handle_excle(r"E:\pythoncode\Python基础\day12\apicase.xlsx","Sheet1")
	data = db.read()
	print(data)
	db.write(5,5,"123")

posted @ 2022-11-25 19:21  进击的bug~  阅读(48)  评论(0编辑  收藏  举报