导入 Pandas 包 import pandas as pd 输入 读取数据表 #总表 total=pd.read_excel('C:/Users/Kinglake/Desktop/1.xls',index_col=0,header=5)# header=5表示从第5行开始,,且表头为第5行的内容 #total.head() #新表 new=pd.read_excel('2.xlsx',index_col=0)# 添加index_col=0,可以在读数据时就去除表中默认的index #new.head() 业务代码 1. 逐行判断 新表的身份证号 是否出现在总表中,返回长度为新表长度的 true false 序列 ifArr=new.身份证号.isin(total.身份证号) 2. 标红样式函数,style.applymap() 需要调用 def color_negative_red(val): color = 'red' return 'color: %s' % color 3.生成“标红代码” mycode="new.style" for i in range(1,len(new)): if ifArr[i]: #print(i) mycode=mycode+'.applymap(color_negative_red,subset=pd.IndexSlice['+str(i)+':'+str(i)+',])' #print(mycode) 输出 1.执行“标红代码”,生成 hightLightRepeat.xlsx 文件 #添加导出为Excel代码 mycode=mycode+'.to_excel("C:/Users/Kinglake/Desktop/hightLightRepeat.xlsx")' #执行拼接好的字符串中的代码 exec(mycode) 2.剔除新表相对于总表的重复项,生成 deleteRepeat.xlsx 文件 new[~new.身份证号.isin(total.身份证号)].to_excel("C:/Users/Kinglake/Desktop/deleteRepeat.xlsx") 所有代码执行完毕 print('---完成!---')