Aug-26-2023, 06:50 PM
I came to ask about the same exercise as last time, with which you helped me and it was very helpful but I would like to know if it could be done in another way in which the comparison of the two excel tables appears in one of the two sheets already created instead of creating a new sheet. I leave here the code.
import openpyxl from openpyxl.styles import PatternFill datafile1 = openpyxl.load_workbook("C:\\Users\\ime1s\\Downloads\\MiCalculo.xlsx") fill_style1 = PatternFill(start_color='FDD835', end_color='FDD835', fill_type='solid') fill_style2 = PatternFill(start_color='cc0000', end_color='cc0000', fill_type='solid') data_sheet1 = datafile1['Comfica Calculo'] data_sheet2 = datafile1['Mi Calculo'] # Create a new sheet for comparison results comparison_sheet = datafile1.create_sheet('Comparison Results') # Iterate through rows in both sheets and compare values for row1, row2 in zip( data_sheet1.iter_rows(min_row=1, max_row=data_sheet1.max_row), data_sheet2.iter_rows(min_row=1, max_row=data_sheet2.max_row), ): for cell1, cell2 in zip(row1, row2): if cell1.value == cell2.value: comparison_sheet.cell(row=cell1.row, column=cell1.column, value=1).fill = fill_style1 else: comparison_sheet.cell(row=cell1.row, column=cell1.column, value=0).fill = fill_style2 datafile1.save('compared_file.xlsx')