I've found a function that compares Excel spreadsheets and highlights changes. How can I modify the script in a way that only changed words will be highlighted?
Below you can see the logic I would like to apply.
The original script is on GitHub, link is here. As an output I would like to keep everything in 1 workbook as in code below. The only change that needs to be made is to somehow mark all changed words and export them to DataFrame 3 (dfDiff).
import pandas as pd
from pathlib import Path
import os
def excel_diff(path_OLD, path_NEW, index_col):
df_OLD = pd.read_excel(path_OLD, index_col=index_col).fillna(0)
df_NEW = pd.read_excel(path_NEW, index_col=index_col).fillna(0)
# Perform Diff
dfDiff = df_NEW.copy()
droppedRows = []
newRows = []
cols_OLD = df_OLD.columns
cols_NEW = df_NEW.columns
sharedCols = list(set(cols_OLD).intersection(cols_NEW))
for row in dfDiff.index:
if (row in df_OLD.index) and (row in df_NEW.index):
for col in sharedCols:
value_OLD = df_OLD.loc[row, col]
value_NEW = df_NEW.loc[row, col]
if value_OLD == value_NEW:
dfDiff.loc[row, col] = df_NEW.loc[row, col]
else:
dfDiff.loc[row, col] = ('{}→{}').format(value_OLD, value_NEW)
else:
newRows.append(row)
for row in df_OLD.index:
if row not in df_NEW.index:
droppedRows.append(row)
dfDiff = dfDiff.append(df_OLD.loc[row, :])
dfDiff = dfDiff.sort_index().fillna('')
print(dfDiff)
print('\nNew Rows: {}'.format(newRows))
print('Dropped Rows: {}'.format(droppedRows))
# Save output and format
# fname = '{} vs {}.xlsx'.format(path_OLD.stem, path_NEW.stem)
fname = (os.path.dirname(os.path.abspath(__file__)) + '/uploads/differences.xlsx'.format(path_OLD.stem, path_NEW.stem))
writer = pd.ExcelWriter(fname, engine='xlsxwriter')
dfDiff.to_excel(writer, sheet_name='differences', index=True)
df_NEW.to_excel(writer, sheet_name=path_NEW.stem, index=True)
df_OLD.to_excel(writer, sheet_name=path_OLD.stem, index=True)
# get xlsxwriter objects
workbook = writer.book
worksheet = writer.sheets['differences']
worksheet.hide_gridlines(2)
worksheet.set_default_row(15)
# define formats
grey_fmt = workbook.add_format({'font_color': '#E0E0E0'})
highlight_fmt = workbook.add_format({'font_color': '#ff6666', 'bg_color': '#ffff00'})
new_fmt = workbook.add_format({'font_color': '#32CD32', 'bold': True})
# set format over range
## highlight changed cells
worksheet.conditional_format('A1:ZZ1000', {'type': 'text',
'criteria': 'containing',
'value': '→',
'format': highlight_fmt})
# highlight new/changed rows
for row in range(dfDiff.shape[0]):
if row + 1 in newRows:
worksheet.set_row(row + 1, 15, new_fmt)
if row + 1 in droppedRows:
worksheet.set_row(row + 1, 15, grey_fmt)
# save
writer.save()
print('\nDone.')
def main():
path_OLD = Path(os.path.dirname(os.path.abspath(__file__)) + '/uploads/old_content.xlsx')
path_NEW = Path(os.path.dirname(os.path.abspath(__file__)) + '/uploads/new_content.xlsx')
# get index col from data
df = pd.read_excel(path_NEW)
index_col = df.columns[0]
print('\nIndex column: {}\n'.format(index_col))
excel_diff(path_OLD, path_NEW, index_col)
main()



