excel_compare/scripts/compare.py
def cli(process, old, new, ui, clean):
    """
        Excel Workbook row-by-row comparison.
        
        \b
        Process by comparing all sheets between two xls/xlsx files or 
        comparing all csv files between 2 folders.

        \b
        When option --process is "folder". It receive 2 arguments:
        - First: The folder containing the old csv files to be compared
        - Second: The folder containing the new csv files to be compared

        \b
        When option --process is "file". It receive 2 arguments:
        - First: The file path with the old xls/xlsx version
        - Second: The file path with the new xls/xlsx version
    """

    missing_sheets = []
    different_sheets = []

    def color_diff(diff, clean):
        """
            Color the differences from difflib library
            :param fname: file path
            :return: checksum string
        """
        for line in diff:
            if line.startswith('+'):
                yield f"{Fore.GREEN}{line}{Fore.RESET}"
            elif line.startswith('-'):
                yield f"{Fore.RED}{line}{Fore.RESET}"
            elif line.startswith('^'):
                yield f"{Fore.BLUE}{line}{Fore.RESET}"
            elif line.startswith('@'):
                yield f"{Fore.BLUE}{line}{Fore.RESET}"
            else:
                if not clean:
                    yield line

    def md5(fname):
        """
            Checksum generator
            :param fname: file path
            :return: checksum string
        """
        hash_md5 = hashlib.md5()
        with open(fname, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                hash_md5.update(chunk)
        return hash_md5.hexdigest()

    def folder_process(old, new, ui, clean):
        missing_sheets = []
        files_old = [f for f in listdir(old) if isfile(join(old, f))]
        files_new = [f for f in listdir(new) if isfile(join(new, f))]

        if set(files_old).difference(set(files_new)) != set():
            missing_sheets = list(set(files_old).difference(set(files_new)))

        # Main
        # Checking the files located in old folder against files in new_folder

        for file_folder in files_old:
            if file_folder in files_new:
                if md5(f"{old}/{file_folder}") == md5(f"{new}/{file_folder}"):
                    print(f"{Fore.MAGENTA}[NO_CHANGES]:{Fore.RESET} {file_folder}")
                else:
                    print(f"{Fore.YELLOW}[DIFFERENT]:{Fore.RESET} {file_folder}")
                    with open(f"{old}/{file_folder}", encoding="UTF-8", errors="ignore") as f_old:
                        f_old_text = f_old.readlines()
                    with open(f"{new}/{file_folder}", encoding="UTF-8", errors="ignore") as f_new:
                        f_new_text = f_new.readlines()
                    
                    # Calculate the differences
                    if ui == True:
                        try:
                            subprocess.run(["/usr/bin/tkdiff", f'{old}/{file_folder}', f'{new}/{file_folder}'])
                        except Exception as e:
                            print(f"\n{Fore.RED}ERROR: {e}\n{Fore.YELLOW}This option is only valid for linux and requires tkdiff. Please install tkdiff by:{Fore.RESET}\n\nsudo apt-get install tkdiff\n")
                            exit(1)

                    else:
                        diff = difflib.unified_diff(f_old_text, f_new_text, fromfile=f"{old}/{file_folder}", tofile=f"{new}/{file_folder}", lineterm='')
                        diff = color_diff(diff, clean)
                        print('\n'.join(diff))

                    # Add different files into a list
                    different_sheets.append(file_folder)
        return missing_sheets, different_sheets

    def file_process(filename):
        print(f"Procesing File: {filename}")
        return convert_excel_csv(filename)
    
    if process == 'folder':
        try:
            missing_sheets, different_sheets = folder_process(old, new, ui, clean)
        except Exception as e:
            print(f"ERROR: {e}")
            exit(1)

    elif process == 'file':
        try:
            csv_old = file_process(old)
            csv_new = file_process(new)
            missing_sheets, different_sheets = folder_process(csv_old, csv_new, ui, clean)
        except Exception as e:
            print(f"ERROR: {e}")
            clean_temp([f"/tmp/{old}", f"/tmp/{new}"])
            exit(1)
        finally:
            clean_temp([f"/tmp/{old}", f"/tmp/{new}"])
    else:
        print(f"option -p or --process must be file or folder. Used: {process}")


    # Report
    print(f"\n\n{Fore.YELLOW}*** REPORT ***{Fore.RESET}")
    print(f"Different Sheets: {different_sheets}")
    print(f"Missing Sheets: {missing_sheets}")

        Excel Workbook row-by-row comparison.
        
        \b
        Process by comparing all sheets between two xls/xlsx files or 
        comparing all csv files between 2 folders.

        \b
        When option --process is "folder". It receive 2 arguments:
        - First: The folder containing the old csv files to be compared
        - Second: The folder containing the new csv files to be compared

        \b
        When option --process is "file". It receive 2 arguments:
        - First: The file path with the old xls/xlsx version
        - Second: The file path with the new xls/xlsx version
    
Excel Workbook row-by-row comparison.
\b
Process by comparing all sheets between two xls/xlsx files or
comparing all csv files between 2 folders.

\b
When option --process is "folder". It receive 2 arguments:
First: The folder containing the old csv files to be compared
Second: The folder containing the new csv files to be compared

\b
When option --process is "file". It receive 2 arguments:
First: The file path with the old xls/xlsx version
Second: The file path with the new xls/xlsx version
Excel Workbook row - by - row comparison . \ b Process by comparing all sheets between two xls / xlsx files or comparing all csv files between 2 folders . \ b When option -- process is " folder " . It receive 2 arguments : First : The folder containing the old csv files to be compared Second : The folder containing the new csv files to be compared \ b When option -- process is " file " . It receive 2 arguments : First : The file path with the old xls / xlsx version Second : The file path with the new xls / xlsx version
def cli(process, old, new, ui, clean):
    missing_sheets = []
    different_sheets = []
    def color_diff(diff, clean):
        for line in diff:
            if line.startswith('+'):
                yield f"{Fore.GREEN}{line}{Fore.RESET}"
            elif line.startswith('-'):
                yield f"{Fore.RED}{line}{Fore.RESET}"
            elif line.startswith('^'):
                yield f"{Fore.BLUE}{line}{Fore.RESET}"
            elif line.startswith('@'):
                yield f"{Fore.BLUE}{line}{Fore.RESET}"
            else:
                if not clean:
                    yield line
    def md5(fname):
        hash_md5 = hashlib.md5()
        with open(fname, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                hash_md5.update(chunk)
        return hash_md5.hexdigest()
    def folder_process(old, new, ui, clean):
        missing_sheets = []
        files_old = [f for f in listdir(old) if isfile(join(old, f))]
        files_new = [f for f in listdir(new) if isfile(join(new, f))]
        if set(files_old).difference(set(files_new)) != set():
            missing_sheets = list(set(files_old).difference(set(files_new)))
        for file_folder in files_old:
            if file_folder in files_new:
                if md5(f"{old}/{file_folder}") == md5(f"{new}/{file_folder}"):
                    print(f"{Fore.MAGENTA}[NO_CHANGES]:{Fore.RESET} {file_folder}")
                else:
                    print(f"{Fore.YELLOW}[DIFFERENT]:{Fore.RESET} {file_folder}")
                    with open(f"{old}/{file_folder}", encoding="UTF-8", errors="ignore") as f_old:
                        f_old_text = f_old.readlines()
                    with open(f"{new}/{file_folder}", encoding="UTF-8", errors="ignore") as f_new:
                        f_new_text = f_new.readlines()
                    if ui == True:
                        try:
                            subprocess.run(["/usr/bin/tkdiff", f'{old}/{file_folder}', f'{new}/{file_folder}'])
                        except Exception as e:
                            print(f"\n{Fore.RED}ERROR: {e}\n{Fore.YELLOW}This option is only valid for linux and requires tkdiff. Please install tkdiff by:{Fore.RESET}\n\nsudo apt-get install tkdiff\n")
                            exit(1)
                    else:
                        diff = difflib.unified_diff(f_old_text, f_new_text, fromfile=f"{old}/{file_folder}", tofile=f"{new}/{file_folder}", lineterm='')
                        diff = color_diff(diff, clean)
                        print('\n'.join(diff))
                    different_sheets.append(file_folder)
        return missing_sheets, different_sheets
    def file_process(filename):
        print(f"Procesing File: {filename}")
        return convert_excel_csv(filename)
    if process == 'folder':
        try:
            missing_sheets, different_sheets = folder_process(old, new, ui, clean)
        except Exception as e:
            print(f"ERROR: {e}")
            exit(1)
    elif process == 'file':
        try:
            csv_old = file_process(old)
            csv_new = file_process(new)
            missing_sheets, different_sheets = folder_process(csv_old, csv_new, ui, clean)
        except Exception as e:
            print(f"ERROR: {e}")
            clean_temp([f"/tmp/{old}", f"/tmp/{new}"])
            exit(1)
        finally:
            clean_temp([f"/tmp/{old}", f"/tmp/{new}"])
    else:
        print(f"option -p or --process must be file or folder. Used: {process}")
    print(f"\n\n{Fore.YELLOW}*** REPORT ***{Fore.RESET}")
    print(f"Different Sheets: {different_sheets}")
    print(f"Missing Sheets: {missing_sheets}")
def cli ( process , old , new , ui , clean ) : missing_sheets = [ ] different_sheets = [ ] def color_diff ( diff , clean ) : """
            Color the differences from difflib library
            :param fname: file path
            :return: checksum string
        """ for line in diff : if line . startswith ( '+' ) : yield f"{Fore.GREEN}{line}{Fore.RESET}" elif line . startswith ( '-' ) : yield f"{Fore.RED}{line}{Fore.RESET}" elif line . startswith ( '^' ) : yield f"{Fore.BLUE}{line}{Fore.RESET}" elif line . startswith ( '@' ) : yield f"{Fore.BLUE}{line}{Fore.RESET}" else : if not clean : yield line def md5 ( fname ) : """
            Checksum generator
            :param fname: file path
            :return: checksum string
        """ hash_md5 = hashlib . md5 ( ) with open ( fname , "rb" ) as f : for chunk in iter ( lambda : f . read ( 4096 ) , b"" ) : hash_md5 . update ( chunk ) return hash_md5 . hexdigest ( ) def folder_process ( old , new , ui , clean ) : missing_sheets = [ ] files_old = [ f for f in listdir ( old ) if isfile ( join ( old , f ) ) ] files_new = [ f for f in listdir ( new ) if isfile ( join ( new , f ) ) ] if set ( files_old ) . difference ( set ( files_new ) ) != set ( ) : missing_sheets = list ( set ( files_old ) . difference ( set ( files_new ) ) ) for file_folder in files_old : if file_folder in files_new : if md5 ( f"{old}/{file_folder}" ) == md5 ( f"{new}/{file_folder}" ) : print ( f"{Fore.MAGENTA}[NO_CHANGES]:{Fore.RESET} {file_folder}" ) else : print ( f"{Fore.YELLOW}[DIFFERENT]:{Fore.RESET} {file_folder}" ) with open ( f"{old}/{file_folder}" , encoding = "UTF-8" , errors = "ignore" ) as f_old : f_old_text = f_old . readlines ( ) with open ( f"{new}/{file_folder}" , encoding = "UTF-8" , errors = "ignore" ) as f_new : f_new_text = f_new . readlines ( ) if ui == True : try : subprocess . run ( [ "/usr/bin/tkdiff" , f'{old}/{file_folder}' , f'{new}/{file_folder}' ] ) except Exception as e : print ( f"\n{Fore.RED}ERROR: {e}\n{Fore.YELLOW}This option is only valid for linux and requires tkdiff. Please install tkdiff by:{Fore.RESET}\n\nsudo apt-get install tkdiff\n" ) exit ( 1 ) else : diff = difflib . unified_diff ( f_old_text , f_new_text , fromfile = f"{old}/{file_folder}" , tofile = f"{new}/{file_folder}" , lineterm = '' ) diff = color_diff ( diff , clean ) print ( '\n' . join ( diff ) ) different_sheets . append ( file_folder ) return missing_sheets , different_sheets def file_process ( filename ) : print ( f"Procesing File: {filename}" ) return convert_excel_csv ( filename ) if process == 'folder' : try : missing_sheets , different_sheets = folder_process ( old , new , ui , clean ) except Exception as e : print ( f"ERROR: {e}" ) exit ( 1 ) elif process == 'file' : try : csv_old = file_process ( old ) csv_new = file_process ( new ) missing_sheets , different_sheets = folder_process ( csv_old , csv_new , ui , clean ) except Exception as e : print ( f"ERROR: {e}" ) clean_temp ( [ f"/tmp/{old}" , f"/tmp/{new}" ] ) exit ( 1 ) finally : clean_temp ( [ f"/tmp/{old}" , f"/tmp/{new}" ] ) else : print ( f"option -p or --process must be file or folder. Used: {process}" ) print ( f"\n\n{Fore.YELLOW}*** REPORT ***{Fore.RESET}" ) print ( f"Different Sheets: {different_sheets}" ) print ( f"Missing Sheets: {missing_sheets}" )
Excel Workbook row-by-row comparison.
Excel Workbook row - by - row comparison .
"""
        Excel Workbook row-by-row comparison.
        
        \b
        Process by comparing all sheets between two xls/xlsx files or 
        comparing all csv files between 2 folders.

        \b
        When option --process is "folder". It receive 2 arguments:
        - First: The folder containing the old csv files to be compared
        - Second: The folder containing the new csv files to be compared

        \b
        When option --process is "file". It receive 2 arguments:
        - First: The file path with the old xls/xlsx version
        - Second: The file path with the new xls/xlsx version
    """ """
            Color the differences from difflib library
            :param fname: file path
            :return: checksum string
        """ """
            Checksum generator
            :param fname: file path
            :return: checksum string
        """ # Main # Checking the files located in old folder against files in new_folder # Calculate the differences # Add different files into a list # Report
process
old
new
ui
clean