从源表提取相应信息到目标表格
# -*- coding: UTF-8 -*- from openpyxl import load_workbook from openpyxl import Workbook #源表载入wb_sorce中 wb_sorce=load_workbook(filename='source_table.xlsx',read_only=True) #待处理的目标表格载入wb_object中 wb_object = load_workbook('object_table.xlsx') #从源表wb_sorce中读取源数据所在的sheet ws_sorce = wb_sorce['一中'] #目标表里读取待处理的sheet ws_object = wb_object['花名册'] # 根据姓名(源表sheet第3列,目标表sheet第5列)匹配查找和写入目标数据身份证号(源表sheet第5列,目标表sheet第10列) for i in range(4,1765): cell_match_object = ws_object.cell(row=i, column=5) cell_target_object = ws_object.cell(row=i, column=10) for j in range(4,1765): cell_match_sorce = ws_sorce.cell(row=j, column=3) cell_target_sorce = ws_sorce.cell(row=j, column=5) vm_s = str(cell_match_sorce.value) vm_t = str(cell_match_object.value) vt_s = str(cell_target_sorce.value) print('正在处理',i,j) if vm_t == vm_s: cell_target_object.value = vt_s print(f'正好是{i}和{j}') break else: continue wb_object.save('object_table.xlsx')