首页 > 其他分享 >excle文件的封装处理-Handle_excle

excle文件的封装处理-Handle_excle

时间:2022-11-25 19:34:14浏览次数:41  
标签:__ 封装 name self file workbook 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")

标签:__,封装,name,self,file,workbook,Handle,excle
From: https://www.cnblogs.com/gxj521test/p/16926140.html

相关文章