Python Excel Highlight Cell Differences

血红的双手。 提交于 2019-12-24 03:35:35

问题


preface: I'm new and self taught. This is my first coding project. I know it's terrible. I'm going to rewrite it once it's complete and working.

I'm trying to write a python script that will compare 2 excel files and highlight the cells that are different. I can print out the differences (using pandas) and highlight a cell (only by hard coding a specific cell). I can't figure out how to highlight cells based on the printed out differences.

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)

This is only the part of the code I'm stuck on. I can post the rest if necessary.


回答1:


You can use the style to add highlighting to your dataframe in pandas.

df2.style.apply(highlight_differences)

Then you can write a function which sets the highlighting criteria

def highlight_differences():
    # check for differences here
    return ['background-color: yellow']


来源:https://stackoverflow.com/questions/43789051/python-excel-highlight-cell-differences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!