P
P
pro100ckat2019-03-29 23:57:31
Python
pro100ckat, 2019-03-29 23:57:31

How to compare two excel files via python?

There are two excel files. One template, the other regular. The first file and the template file have the same static structure, with the only difference being that the cell values ​​may differ. You need to check the template values ​​with the values ​​in the first file. For work, I use the openpyxl library. For me, the difficulty is that you need to check the given range of cells, although it is known in advance and does not change. There are several such ranges. If the template value does not match the value of file 1, then you should replace the value of file 1 with the value from the template. The image shows a table that needs to be compared with another table of the same structure. Cells that need to be compared are highlighted.
5c9e868114af7851817137.jpeg
So, I wrote the code, but I can’t figure out how to make one by one compare the range of cells from the first book with the range of cells from the second. Confused about nested loops. The code is obviously not working correctly. The comparisons are wrong. First, let's take a small range as an example. Further, by analogy, I will complete the code for the remaining ranges.

import openpyxl
from openpyxl.cell import Cell
wb = openpyxl.load_workbook('./09.03.02_ТИИвБ_ИКБСП_2019.xlsx') #открыли исходный файл
sheet_wb = wb['График'] #выбрали лист 1 файла
shablon = openpyxl.load_workbook('C:/Users/user/Desktop/check.xlsx') #открыли шаблон
sheet_shablon = shablon['График'] #выбрали лист 2 файла
for i in sheet_wb['B13':'J13']: #Обращаемся к диапазону из 1 листа
    for cell1 in i: #Обращаемся к значениям из этого диапзона
        for j in sheet_shablon['B13':'J13']:
                for cell2 in j:
                    if cell1.value != cell2.value:
                        cell1.value == cell2.value
wb.save('test1.xlsx')

Tried like this, obviously not right
sheet_wb = wb['График']
sheet_shablon = shablon['График']
for i in sheet_wb['B13':'E13']:
    for cell in i:
        if sheet_wb[cell].value != sheet_shablon[cell].value:
            sheet_wb[cell].value == sheet_shablon[cell].value

Please tell me how to implement correctly. thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pro100ckat, 2019-03-30
@pro100ckat

The solution is this. We write the template data to the list. Compare 1 file with the values ​​from the list.

sheet_wb = wb['График']
sheet_shablon = shablon['График']
list = []
for i in sheet_shablon['B13':'BA39']:
    for cell in i:
        list.append(cell.value)
j=0
for i in sheet_wb['B13':'BA39']:
    for cell in i:
        if cell.value != list[j]:
            cell.value = list[j]
            j = j+1
        else:
            j = j+1
wb.save('test1.xlsx')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question