赞
踩
import openpyxl
from openpyxl import load_workbook
wb = openpyxl.load_workbook(filepath)
ws = wb['Sheet1']
max_row = ws.max_row
#待匹配列表
targetlist=['A','B','CC']
to_del_rows = []
#1、按列查找单元格的值是否在目标看列表中,不符合的删除所在行,把要删除的行先填入待删除列表
for row in range(1,max_row+1):
if str(ws.cell(row,1).value) not in targetlist:#以第一列匹配为例
to_del_rows.append(row)
#2、逆序删除,避免重复重排序后的空行
for row in reversed(to_del_rows):
ws.delete_rows(row)
#保存
wb.save(filepath)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。