跨表批量删除

我有一个Excel,有多个sheet,其中一个sheet 匹配配置中有一个控制是否存在的选项,同时使用localeId控制个sheet的联系

现在要删除所有符合的locaeId的数据

import openpyxl
wb=openpyxl.load_workbook(r'E:\shenghe\excels\branch\Beta\1\房间配置.xlsx')
sheet_names=wb.sheetnames

# get the unableshow race id
sheet=wb['匹配配置']
row_max=sheet.max_row
col_max=sheet.max_column
id_list=[]
row_i=9 
while row_i <= row_max+1:
    if sheet.cell(row=row_i,column=11).value==0:
        id_list.append(sheet.cell(row=row_i,column=1).value)
        print('删除'+str(row_i)+'')
        sheet.delete_rows(row_i)
        row_max=row_max-1
        pass
    else:
        row_i=row_i+1
        pass
    pass
print('删除'+str(sheet))
sheet_names.remove('匹配配置') #名称列表中删除已经处理的表格
for sheet_1 in sheet_names:
    sheet_i=wb[sheet_1]
    col_max_i=sheet_i.max_column
    row_max_i=sheet_i.max_row
    for get_name in range(1,col_max+1):

        if sheet_i.cell(row=7,column=get_name).value=='localeId':
            print(get_name)
            break
        else:
            pass
        pass
    row_i=1# 行从头开始循环
    while row_i <= row_max_i+1:
        if sheet_i.cell(row=row_i,column=get_name).value in id_list:
            print('删除'+str(row_i)+'')
            sheet_i.delete_rows(row_i)
            row_max_i=row_max_i-1
            pass
        else:
            row_i=row_i+1
            pass
        pass
    print('处理完'+str(sheet_i))
wb.save('删除.xlsx')

猜你喜欢

转载自www.cnblogs.com/yuvejxke/p/12938047.html