python3 openpyxl 操作excel


def
checkCorePoint(self): wb = load_workbook(self.targetCoreFile) sheets = wb.sheetnames # 定义一个存放的sheet targetSheet = 'TargetCorePoint' # 判断是否已经存在这个sheet页,如果不存在新建一个 if targetSheet not in sheets: cs = wb.create_sheet(targetSheet) else: # 如果已经存在了,那么删除旧的,防止数据堆积,重新创建新的sheet页 cs = wb[targetSheet] wb.remove(cs) cs = wb.create_sheet(targetSheet) sheet = sheets[0] ws = wb[sheet] targetPointList = [] # 遍历读取所有行 for row in ws.rows: line = [col.value for col in row] # 讲所读信息存在list targetPointList.append(line) targetCorePointList = [['business_ch_name', 'category_id', 'target_id', 'event_type', '是否核心埋点']] # 遍历埋点list,判断是否在已知当中 for i in targetPointList: # 如果存在当前信息增加标识 if i[2] in self.sourceCorePointList: i.append('') # 并把该埋点存放到list targetCorePointList.append(i) # 遍历list,并将存放到 TargetCorePoint sheet 页 for row in targetCorePointList: cs.append(row) wb.save(filename=self.targetCoreFile) print("执行完成!")

参考 :Python - openpyxl 读写操作Excel

原文地址:https://www.cnblogs.com/BlueSkyyj/p/13860545.html