前言:我是新人,自学成才。这是我的第一个编码项目。我知道这很糟糕。一旦完成并工作,我将重写它。
我正在尝试编写一个 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']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)