Created
July 24, 2023 14:32
-
-
Save jilleb/a7fcae4bfd9a54711794fd6563b5b3dd to your computer and use it in GitHub Desktop.
Compare columns of 2 Excel files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import argparse | |
def compare_excels(excel_file1, column_index1, excel_file2, column_index2): | |
try: | |
# Read the Excel files into pandas DataFrames | |
df1 = pd.read_excel(excel_file1) | |
df2 = pd.read_excel(excel_file2) | |
# Extract the values from the specified columns in Excel 1 and Excel 2 | |
values_range1 = set(df1.iloc[:, column_index1]) | |
values_range2 = set(df2.iloc[:, column_index2]) | |
# Find the values present in range1 but missing in range2 | |
missing_values = values_range1.difference(values_range2) | |
# Print the missing values, if any | |
if missing_values: | |
print("Values missing in Excel 2:") | |
for value in missing_values: | |
print(value) | |
else: | |
print("No missing values found.") | |
except Exception as e: | |
print("Error:", str(e)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="Compare two Excel files and find missing values.") | |
parser.add_argument("file1", help="Path to the first Excel file.") | |
parser.add_argument("column_index1", type=int, help="Column index for File 1 (0-based index).") | |
parser.add_argument("file2", help="Path to the second Excel file.") | |
parser.add_argument("column_index2", type=int, help="Column index for File 2 (0-based index).") | |
args = parser.parse_args() | |
compare_excels(args.file1, args.column_index1, args.file2, args.column_index2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
python excel_compare.py file1.xlsx 0 file2.xlsx 0