Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.8 kB
1
Indexable
Never
import pandas as pd
import tkinter as tk
from tkinter import filedialog, messagebox
from openpyxl.styles import PatternFill

def browse_files():
    file_paths = filedialog.askopenfilenames(filetypes=[("Excel Files", "*.xlsx")])
    if len(file_paths) < 2:
        messagebox.showerror("Error", "Please select at least two Excel files.")
        return
    process_files(file_paths)

def process_files(file_paths):
    try:
        dfs = [pd.read_excel(file_path) for file_path in file_paths]

        if not all(df.columns.tolist() == dfs[0].columns.tolist() for df in dfs):
            messagebox.showerror("Error", "Not all files have the same columns.")
            return

        unique_identifier = ['Requirement(s)', 'Specification(s)']
        merged_df = pd.merge(*dfs, on=unique_identifier, how='outer', suffixes=tuple(f"_file{i+1}" for i in range(len(dfs))))
        discrepancy_df = merged_df[merged_df.apply(lambda x: not x[::2].equals(x[1::2]), axis=1)]

        with pd.ExcelWriter('discrepancies.xlsx', engine='openpyxl') as writer:
            discrepancy_df.to_excel(writer, sheet_name='Discrepancies', index=False)
            wb = writer.book
            ws = wb['Discrepancies']
            red_fill = PatternFill(start_color='FFFF0000', end_color='FFFF0000', fill_type='solid')
            for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=1, max_col=ws.max_column):
                for cell in row:
                    if cell.column_letter.endswith('_file1') and cell.value != cell.offset(column=1).value:
                        cell.fill = red_fill
                    elif cell.column_letter.endswith('_file2') and cell.value != cell.offset(column=-1).value:
                        cell.fill = red_fill

        # Compare values in Column C and Column D
        merged_df['Column6'] = merged_df.apply(lambda row: ', '.join(set(row['Column3'].split(', ')) - set(row['Column4'].split(', '))), axis=1)
        merged_df['Column7'] = merged_df.apply(lambda row: ', '.join(set(row['Column4'].split(', ')) - set(row['Column3'].split(', '))), axis=1)
        merged_df['Column8'] = merged_df.apply(lambda row: ', '.join(set(row['Column3'].split(', ')) & set(row['Column4'].split(', '))), axis=1)

        merged_df.to_excel('output_file.xlsx', index=False)

        messagebox.showinfo("Success", "Output saved to 'output_file.xlsx'. Discrepancies exported to 'discrepancies.xlsx'.")
    except Exception as e:
        messagebox.showerror("Error", str(e))

# Create the main application window
app = tk.Tk()
app.title("Excel Discrepancy Checker")

# Create a button to browse files
browse_button = tk.Button(app, text="Browse Files", command=browse_files)
browse_button.pack(padx=20, pady=10)

# Run the Tkinter event loop
app.mainloop()
Leave a Comment