excle文件的封装处理-Handle_excle()

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")

————————
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")