-
Notifications
You must be signed in to change notification settings - Fork 0
/
exdifftool.py
163 lines (135 loc) · 5.19 KB
/
exdifftool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import xlrd
import csv
import os
import logging
from os import sys
# convert excel files to csv and write csv files
def csv_from_excel(excel_file):
worksheets = []
filenames = []
workbook = xlrd.open_workbook(excel_file)
all_worksheets = workbook.sheet_names()
for worksheet_name in all_worksheets:
worksheet = workbook.sheet_by_name(worksheet_name)
worksheets.append(worksheet_name)
filename = u'{}_{}.csv'.format(excel_file, worksheet_name)
filenames.append(filename)
with open(filename, 'w') as your_csv_file:
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL, lineterminator='\n')
for rownum in range(worksheet.nrows):
wr.writerow([entry for entry in worksheet.row_values(rownum)])
written_csvs = {excel_file : worksheets}
return written_csvs, filenames
def get_sheet_names(excel_file):
written_csvs, filenames = csv_from_excel(excel_file)
remove_csvs(filenames)
return list(written_csvs.values())[0]
# find matching sheets
def filter_diffs(files1, files2):
diffs = []
for excel1, sheets1 in files1.items():
for excel2, sheets2 in files2.items():
for sheet1 in sheets1:
for sheet2 in sheets2:
if sheet1 == sheet2:
filename1 = u'{}_{}.csv'.format(excel1, sheet1)
filename2 = u'{}_{}.csv'.format(excel2, sheet2)
t = (filename1, filename2)
diffs.append(t)
return diffs
# returns True if two rows are equal
def lines_equal(line1, line2):
cells1 = line1.split(",")
cells2 = line2.split(",")
cells1 = [i for i in cells1 if i]
cells2 = [i for i in cells2 if i]
if len(cells1) != len(cells2):
return False
for i in range(len(cells1)):
if cells1[i] != cells2[i]:
return False
return True
# finds the cells which are not equal
def neq_details(row_num, line1, line2):
cells1 = line1.split(",")
cells2 = line2.split(",")
details = []
for i in range(max(len(cells1), len(cells2))):
cell1 = ""
cell2 = ""
if i < len(cells1):
cell1 = cells1[i]
if i < len(cells2):
cell2 = cells2[i]
if cell1 != cell2:
details.append("neqd {}:{}".format(row_num, i)) # format: neqd row:col
return details
def do_diff(filename1, filename2):
logging.debug("\n*** do_diff of " + filename1 + " and " + filename2 + " ***")
logging.debug(filename1 + " ************** " + filename2)
logging.debug('-'*60)
diff = []
lines1 = [line.rstrip() for line in open(filename1, 'r')]
lines2 = [line.rstrip() for line in open(filename2, 'r')]
for i in range(max(len(lines1), len(lines2))):
line1 = ""
line2 = ""
if i < len(lines1):
line1 = lines1[i]
if i < len(lines2):
line2 = lines2[i]
# line2 was added in file2 and does not exist in file1
if not line1:
logging.debug('<empty>{}{}:{}'.format(' '*35, i+1, line2))
diff.append("add {}:{}".format("-1", i))
# line1 was added in file1 and does not exist in file2
if not line2:
logging.debug('{}:{}{}<empty>'.format(i+1, line1, ' '*35))
diff.append("add {}:{}".format(i, "-1"))
# both lines are present
if line1 and line2:
if lines_equal(line1, line2):
logging.debug('{}:{} <equal>'.format(i+1, i+1))
diff.append("eq {}:{}".format(i, i))
continue
# check if line was merely moved but still exists
found = False
k = 0
for l in lines2:
if lines_equal(l, line1):
logging.debug('{}:{} <moved> {}:{}'.format(i+1, line1, k, l))
diff.append("mv {}:{}".format(i, k))
found = True
break
k += 1
if found:
continue
# check if content differs
logging.debug('{}:{} <neq> {}:{}'.format(i+1, line1, i+1, line2))
for d in neq_details(i, line1, line2):
diff.append(d)
logging.debug('-'*60)
logging.debug("\n")
logging.debug(diff)
return diff
def get_row_and_cols_of_csv(csv):
rows = len(csv)
cols = []
for r in csv:
cols.append(len(r.split(",")))
return rows, max(cols)
def remove_csvs(filenames):
for f in filenames:
try:
os.remove(f)
except FileNotFoundError:
continue
def run(filename1, filename2, sheet1, sheet2):
files1, filenames1 = csv_from_excel(filename1)
files2, filenames2 = csv_from_excel(filename2)
diffs = filter_diffs(files1, files2)
csv1 = [line.rstrip() for line in open(filenames1[sheet1], 'r')]
csv2 = [line.rstrip() for line in open(filenames2[sheet2], 'r')]
diffs = do_diff(filenames1[sheet1], filenames2[sheet2])
remove_csvs(filenames1 + filenames2)
return csv1, csv2, diffs