что делает код поясни простым языком import os import threading from tkinter import *…
- User
что делает код поясни простым языком import os import threading from tkinter import * from tkinter import ttk from tkinter import filedialog as fd from openpyxl import load_workbook import pandas as pd import configparser from openpyxl import Workbook from openpyxl.styles import PatternFill from tqdm import tqdm from concurrent.futures import ThreadPoolExecutor from multiprocessing import Pool from tkinter.ttk import Progressbar import numpy as np from functools import partial from pandas import isna # Импортировать isna из pandas
config = configparser.ConfigParser() firstTbl, secondTbl, outputTbl, outFolder = "", "", "", ""
if os.path.exists('config.ini'): config.read('config.ini', encoding='utf-8') firstTbl = config.get('Paths', 'first_table', fallback="") secondTbl = config.get('Paths', 'second_table', fallback="") outFolder = config.get('Paths', 'output_folder', fallback="") identifying_columns = config.get( 'Columns', 'identifying_columns', fallback="").split(',') exception_words = config.get( 'Settings', 'exception_words', fallback="").split(',')
def saveConfig(): config['Paths'] = {'first_table': firstTbl, 'second_table': secondTbl, 'output_folder': outFolder} with open('config.ini', 'w', encoding='utf-8') as f: config.write(f)
def threaded_function(target_function, *args): threading.Thread(target=target_function, args=args).start()
def process_rows(identifying_values, df1, df2, all_columns): processed_cells = []
row1 = df1[df1[identifying_columns].apply( tuple, axis=1) == identifying_values] row2 = df2[df2[identifying_columns].apply( tuple, axis=1) == identifying_values] for col in all_columns: fill = None value1 = row1[col].iloc[0] if col in df1.columns and not row1.empty else None value2 = row2[col].iloc[0] if col in df2.columns and not row2.empty else None if isna(value1) and isna(value2): continue if value1 is None and value2 is not None: fill = PatternFill(start_color="00FF00", end_color="00FF00", fill_type="solid") elif value2 is None and value1 is not None: fill = PatternFill(start_color="FF0000", end_color="FF0000", fill_type="solid") elif value1 != value2: fill = PatternFill(start_color="FFFF00", end_color="FFFF00", fill_type="solid") value1 = f"{value1} --> {value2}" processed_cells.append( (col, value1 if value1 is not None else value2, fill)) return row1.index[0] if not row1.empty else row2.index[0], processed_cellsdef diffXlsx(): df1 = pd.read_excel(firstTbl) df2 = pd.read_excel(secondTbl)
all_columns = list(df1.columns) + \ [col for col in df2.columns if col not in df1.columns] unique_identifying_values = set(df1[identifying_columns].apply( tuple, axis=1).unique()).union(df2[identifying_columns].apply(tuple, axis=1).unique()) missing_in_df1 = [col for col in all_columns if col not in df1.columns] missing_in_df2 = [col for col in all_columns if col not in df2.columns] if missing_in_df1: print( f"Предупреждение: Столбцы, отсутствующие в первой таблице: {', '.join(missing_in_df1)}") if missing_in_df2: print( f"Предупреждение: Столбцы, отсутствующие во второй таблице: {', '.join(missing_in_df2)}") outputTbl = os.path.join(outFolder, os.path.basename(firstTbl)) wb = Workbook() ws = wb.active ws.title = "Comparison" total_iterations = len(unique_identifying_values) * len(all_columns) func = partial(process_rows, df1=df1, df2=df2, all_columns=all_columns) for c_idx, col_name in enumerate(all_columns, 1): ws.cell(row=1, column=c_idx, value=col_name) processed_data = [] with tqdm(total=total_iterations, desc="Processing") as pbar: for identifying_values in unique_identifying_values: original_idx, processed_cells = func(identifying_values) processed_data.append((original_idx, processed_cells)) pbar.update(len(all_columns)) processed_data.sort(key=lambda x: x[0]) for r_idx, (original_idx, processed_cells) in enumerate(processed_data, 2): for col, value, fill in processed_cells: col_idx = all_columns.index(col) + 1 cell = ws.cell(row=r_idx, column=col_idx, value=value) if fill: cell.fill = fill wb.save(outputTbl) - Assistant