import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles import PatternFill
# 读取数据
df1 = pd.read_excel(r'C:\Users\zxh\Desktop\2024年待核对信息.xlsx')
df2 = pd.read_excel(r'C:\Users\zxh\Desktop\2022年统计期间的学习经历.xlsx')
# 合并两个数据集
merged_df = df2.merge(df1, on=['人员编码','姓名','学历'], suffixes=('_data1', '_data2'))
# 比较每一行数据的差异
for index, row in merged_df.iterrows():
for col in df1.columns:
if col != '姓名' and col != '人员编码' and col != '学历' and row[col + '_data1'] != row[col + '_data2']:
merged_df.at[index, col] = f"{row[col + '_data1']} -> {row[col + '_data2']}"
# 将结果保存到新的Excel文件中
merged_df.to_excel(r'C:\Users\zxh\Desktop\output_xueli0716.xlsx', index=False)
# 加载新创建的Excel文件并设置黄色底色
wb = load_workbook(r'C:\Users\zxh\Desktop\output_xueli0716.xlsx')
ws = wb.active
yellow_fill = PatternFill(start_color='FFFF00', end_color='FFFF00', fill_type='solid')
for row in ws.iter_rows(min_row=2): # 从第二行开始,因为第一行是列名
for cell in row:
if cell.value and isinstance(cell.value, str) and '->' in cell.value:
cell.fill = yellow_fill
# 保存带有黄色底色的单元格
wb.save(r'C:\Users\zxh\Desktop\output_with_highlight_xueli0716.xlsx')
python实现对比两个相同列数据中的差异并另存标黄
于 2024-07-16 14:19:46 首次发布