import os
import traceback
from openpyxl import *
from logs.login import loggin
from openpyxl.reader.excel import load_workbook
class read_data_calss:
file_name = r'../../Data/data.xlsx' #在当前路劲执行
video_list = []
try:
'''
读取video的Excle文件视频源数据方法
'''
def read_video_data(self, file_name):
self.object = load_workbook(str(file_name)) #获取到一个文件对象
sheets =self.object.sheetnames #获取文件对象中的所有表名称['video_moude', 'Vehicle_moude'], 注: get_sheet_names()已弃用,
print(sheets)
self.ws = self.object[sheets[0]] #获取video_moude表
print(self.ws)
rows = self.ws.max_row #获取行
columns = self.ws.max_column #获取列
for i in range(2, rows+1): #从第二行开始读取(因为第一行是标题)
for j in range(1, columns+1): #获取列(第一列开始读取)
cell_value = self.ws.cell(row=i, column=j).value
read_data_calss.video_list.append(cell_value)
return read_data_calss().video_list
'''
excle写入的方法
'''
def write_data(self, file_name, rw, colum, val):
self.object = load_workbook(str(file_name)) # 获取到一个文件对象
sheet_name = self.object.sheetnames # 获取文件对象中的所有表名称['video_moude', 'Vehicle_moude'], 注: get_sheet_names()已弃用,
print(sheet_name)
sheet = self.object[sheet_name[0]]
print(sheet)
print(file_name, rw, colum, val)
sheet.cell(row=rw, column=colum, value=val)
self.object.save(file_name)
print("写入成功!")
except Exception as e:
print("Exception捕获到现异常了:{0}".format(e))
loggin.log_info(traceback.format_exc())
finally:
pass
#
# if __name__ == '__main__':
# res =read_data_calss().write_data()
#
python读写xlsx文件
于 2024-07-19 10:56:18 首次发布