Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.3 kB
1
Indexable
Never
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox

def process_excel(input_filepath, output_filepath):
    try:
        df = pd.read_excel(input_filepath)
        df.columns = ['LeftColumn1', 'Rightcolumn1']
        
        def find_unique_values(row):
            set1 = set(str(row['LeftColumn1']).strip().split(','))
            set2 = set(str(row['Rightcolumn1']).strip().split(','))
            unique_to_col1 = ', '.join(sorted(set1 - set2))
            unique_to_col2 = ', '.join(sorted(set2 - set1))
            return pd.Series([unique_to_col1, unique_to_col2], index=['Column3', 'Column4'])
        
        df[['Column3', 'Column4']] = df.apply(find_unique_values, axis=1)
        
        def find_common_values(row):
            set1 = set(str(row['LeftColumn1']).strip().split(','))
            set2 = set(str(row['Rightcolumn1']).strip().split(','))
            common_values = ', '.join(sorted(set1.intersection(set2)))
            return common_values
        
        df['Column5'] = df.apply(find_common_values, axis=1)
        
        df = df[['LeftColumn1', 'Rightcolumn1', 'Column3', 'Column4', 'Column5']]
        
        df.to_excel(output_filepath, index=False)
        messagebox.showinfo("Success", "Output saved to '{}'".format(output_filepath))
    except Exception as e:
        messagebox.showerror("Error", str(e))

def select_input_file():
    input_filepath = filedialog.askopenfilename(title="Select Input Excel File", filetypes=[("Excel Files", "*.xlsx")])
    if input_filepath:
        input_entry.delete(0, tk.END)
        input_entry.insert(0, input_filepath)

def select_output_file():
    output_filepath = filedialog.asksaveasfilename(title="Select Output Excel File", defaultextension=".xlsx", filetypes=[("Excel Files", "*.xlsx")])
    if output_filepath:
        output_entry.delete(0, tk.END)
        output_entry.insert(0, output_filepath)

def execute_process():
    input_filepath = input_entry.get()
    output_filepath = output_entry.get()
    if not input_filepath or not output_filepath:
        messagebox.showerror("Error", "Please select input and output files.")
        return
    process_excel(input_filepath, output_filepath)

# Create tkinter window
root = tk.Tk()
root.title("Excel Processing Utility")

# Input File Selection
input_label = tk.Label(root, text="Input Excel File:")
input_label.grid(row=0, column=0, padx=5, pady=5, sticky="w")
input_entry = tk.Entry(root, width=50)
input_entry.grid(row=0, column=1, padx=5, pady=5, sticky="we")
input_button = tk.Button(root, text="Browse", command=select_input_file)
input_button.grid(row=0, column=2, padx=5, pady=5)

# Output File Selection
output_label = tk.Label(root, text="Output Excel File:")
output_label.grid(row=1, column=0, padx=5, pady=5, sticky="w")
output_entry = tk.Entry(root, width=50)
output_entry.grid(row=1, column=1, padx=5, pady=5, sticky="we")
output_button = tk.Button(root, text="Browse", command=select_output_file)
output_button.grid(row=1, column=2, padx=5, pady=5)

# Execute Button
execute_button = tk.Button(root, text="Process Excel", command=execute_process)
execute_button.grid(row=2, column=1, padx=5, pady=10)

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