Untitled

 avatar
unknown
plain_text
a year ago
2.1 kB
6
Indexable
import pandas as pd
import tkinter as tk
from tkinter import filedialog, messagebox
from openpyxl import Workbook
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]
        unique_identifier = ['Requirement(s)', 'Specification(s)']

        # Merge the DataFrames based on the unique identifier(s)
        merged_df = pd.merge(*dfs, on=unique_identifier, how='outer', suffixes=tuple(f"_file{i+1}" for i in range(len(dfs))))

        # Calculate columns C and D
        merged_df['Column3'] = merged_df.apply(lambda row: ', '.join(set(str(row['Column1']).split(', ')) - set(str(row['Column2']).split(', '))) if pd.notna(row['Column1']) and pd.notna(row['Column2']) else '', axis=1)
        merged_df['Column4'] = merged_df.apply(lambda row: ', '.join(set(str(row['Column2']).split(', ')) - set(str(row['Column1']).split(', '))) if pd.notna(row['Column1']) and pd.notna(row['Column2']) else '', axis=1)

        # Add columns E, F, and G
        merged_df['Column5'] = ''
        merged_df['Column6'] = ''
        merged_df['Column7'] = ''

        # Export to Excel
        with pd.ExcelWriter('output_file.xlsx', engine='openpyxl') as writer:
            merged_df.to_excel(writer, sheet_name='Sheet1', index=False)

        messagebox.showinfo("Success", "Output saved to 'output_file.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()
Editor is loading...
Leave a Comment