nourou4them / python-learning

Learn to code in Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Excel check

nourou4them opened this issue · comments

import pandas as pd

def compare_excel_files(file1_path, file2_path, keys):
# Read Excel files
df1 = pd.read_excel(file1_path)
df2 = pd.read_excel(file2_path)

# Get number of lines for each file
num_lines_file1 = len(df1)
num_lines_file2 = len(df2)

# Get differences between columns values for given keys
differences = {}
for key in keys:
    differences[key] = df1[df1[key] != df2[key]]

return num_lines_file1, num_lines_file2, differences

def write_output_to_txt(output_path, num_lines_file1, num_lines_file2, differences):
with open(output_path, 'w') as file:
file.write(f"Number of lines in File 1: {num_lines_file1}\n")
file.write(f"Number of lines in File 2: {num_lines_file2}\n\n")

    for key, diff_df in differences.items():
        file.write(f"Differences for key '{key}':\n")
        if not diff_df.empty:
            file.write(diff_df.to_string(index=False))
        else:
            file.write("No differences found for this key.")
        file.write("\n\n")

if name == "main":
# Input file paths and keys
file1_path = input("Enter path to the first Excel file: ")
file2_path = input("Enter path to the second Excel file: ")
keys = input("Enter keys separated by commas (e.g., 'A,B,C'): ").split(',')

# Analyze files and get differences
num_lines_file1, num_lines_file2, differences = compare_excel_files(file1_path, file2_path, keys)

# Output to txt file
output_path = input("Enter the path to save the output txt file: ")
write_output_to_txt(output_path, num_lines_file1, num_lines_file2, differences)

print("Analysis complete. Output saved to", output_path)