前言:我是新人,自学成才。这是我的第一个编码项目。我知道这很糟糕。一旦它完成并工作,我将重写它。
我正在尝试编写一个 python 脚本来比较 2 个 excel 文件并突出显示不同的单元格。我可以打印出差异(使用 Pandas )并突出显示一个单元格(仅通过对特定单元格进行硬编码)。我不知道如何根据打印出的差异突出显示单元格。
df1 = pd.read_excel(mxln) # Loads master xlsx for comparison
df2 = pd.read_excel(sfcn) # Loads student xlsx for comparison
print('If NaN, correct. If not NaN, incorrect')
difference = df2[df2 != df1] # Scans for differences
print(difference)
lmfh = load_workbook(mxln) # Load mxln for highlight
lsfh = load_workbook(sfcn) # Load sfcn for highlight
lmws = lmfh.active
lsws = lsfh.active
redFill = PatternFill(start_color='FFEE1111', end_color='FFEE1111', fill_type='solid')
lsws['A1'].fill = redFill # Hardcoded cell color
lsfh.save(sfcn)
这只是我坚持使用的代码的一部分。如有必要,我可以发布其余部分。
最佳答案
您可以使用该样式为 pandas 中的数据框添加突出显示。
df2.style.apply(highlight_differences)
然后你可以写一个函数来设置高亮条件
def highlight_differences():
# check for differences here
return ['background-color: yellow']
关于Python Excel 突出显示单元格差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43789051/