Compare 2 excel files using Python

user6241246 picture user6241246 · May 9, 2016 · Viewed 57.5k times · Source

I have two xlsx files as follows:

value1   value2   value3
0.456   3.456    0.4325436
6.24654 0.235435 6.376546
4.26545 4.264543 7.2564523

and

value1   value2  value3
0.456   3.456    0.4325436
6.24654 0.23546  6.376546
4.26545 4.264543 7.2564523

I need to compare all cells, and if a cell from file1 != a cell from file2 print that.

import xlrd
rb = xlrd.open_workbook('file1.xlsx')
rb1 = xlrd.open_workbook('file2.xlsx')
sheet = rb.sheet_by_index(0)
for rownum in range(sheet.nrows):
    row = sheet.row_values(rownum)
    for c_el in row:
        print c_el

How can I add the comparison cell of file1 and file2 ?

Answer

Abbas picture Abbas · May 9, 2016

Use pandas and you can do it as simple as this:

import pandas as pd

df1 = pd.read_excel('excel1.xlsx')
df2 = pd.read_excel('excel2.xlsx')

difference = df1[df1!=df2]
print difference

And the result will look like this:

enter image description here