Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
1
Indexable
Never
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

def load_data(filename):
    wb = openpyxl.load_workbook(filename, read_only=True, keep_links=False)
    data = pd.DataFrame(wb.active.values)
    wb.close()
    return data

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

data_old = {key: load_data(filename) for key, filename in files.items()}

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

    data_new = {key: load_data(filename) 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():
                print(f"Row {index+1} has changes:")
                for col in changes.keys():
                    old, new = changes[col]['self'], changes[col]['other']
                    if pd.isna(old): old = ""
                    if pd.isna(new): new = ""
                    highlighted_changes = highlight_diff(str(old), str(new))
                    print(f"Column {col[0]+1}: {highlighted_changes}")
            
    data_old = data_new