Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.6 kB
0
Indexable
Never
import openpyxl
old_file = 'ICF_classifier_prototype.xlsx'
new_file = 'ICF_classifier_prototype_new.xlsx'
wb_old = openpyxl.load_workbook(old_file, data_only=True)
wb_new = openpyxl.load_workbook(new_file, data_only=True)

def compareTables(table_old : openpyxl.worksheet.table.Table, table_new : openpyxl.worksheet.table.Table):
    #print(table_old)
    cellrange_old = sheet_old[table_old.ref]
    cellrange_new = sheet_new[table_new.ref]
    for row_nr in range(len(cellrange_new)):
        row_old = cellrange_old[row_nr]
        row_new = cellrange_new[row_nr]        
        for cell_nr in range(len(row_new)):
            cell_old = row_old[cell_nr]
            cell_new = row_new[cell_nr]         
            #print(str(cell_old.value)+' '+str(cell_new.value))
            if str(cell_new.value) != str(cell_old.value):
                print(f'     difference found in {cell_new} --> values old/new = {cell_old.value}:{cell_new.value}')
    
    
def compareSheets(sheet_old : openpyxl.worksheet.worksheet.Worksheet, sheet_new : openpyxl.worksheet.worksheet.Worksheet):
    print(f'Comparing worksheet {sheet_old.title}')
    # for each table
    for table in sheet_new.tables:        
        table_old = sheet_old.tables[table]
        table_new = sheet_new.tables[table]
        #print(table_old)
        compareTables(table_old, table_new)        
        
for sheet_new in wb_new.sheetnames:
    sheet_old = wb_old[sheet_new]
    sheet_new = wb_new[sheet_new]
    #print(type(sheet_old))
    compareSheets(sheet_old, sheet_new)
    
wb_old.close()
wb_new.close()