Untitled

 avatar
unknown
plain_text
2 years ago
1.4 kB
2
Indexable
import openpyxl
import pandas as pd
import os
import time
from termcolor import colored
import difflib

def highlight_diff(old, new):
    diff = difflib.ndiff(old, new)
    diff_text = ''.join(diff)
    highlighted = diff_text.replace('-', colored('-', 'green')).replace('+', colored('+', 'green'))
    return highlighted

files = {
    'Line 1': 'file1.xlsx',
    'Line 2': 'file2.xlsx',
    'Line 3': 'file3.xlsx',
}

data_old = {key: pd.DataFrame(openpyxl.load_workbook(filename, read_only=True, keep_links=False).active.values) for key, filename in files.items()}

while True:
    time.sleep(60)  # wait for 60 seconds

    data_new = {key: pd.DataFrame(openpyxl.load_workbook(filename, read_only=True, keep_links=False).active.values) for key, filename in files.items()}

    for title, df_old in data_old.items():
        df_new = data_new[title]

        if not df_old.equals(df_new):
            print(f"Changes in {title}:")
            diff_df = df_old.compare(df_new)

            for index, changes in diff_df.iterrows():
                for col, change in changes.iteritems():
                    old, new = change['self'], change['other']
                    if pd.isna(old): old = ""
                    if pd.isna(new): new = ""
                    highlighted_changes = highlight_diff(str(old), str(new))
                    print(f"Row {index+1}, Column {col+1}: {highlighted_changes}")
            
    data_old = data_new
Editor is loading...