4
4
from openpyxl import load_workbook
5
5
from openpyxl .cell import Cell
6
6
7
- def compare_cells (c1 : Cell , c2 : Cell ):
7
+ case = False
8
+
9
+ def compare_cells (c1 : Cell , c2 : Cell , case : bool ):
8
10
#TODO: ignore whitespace and add case-insensitive comparison
9
- if c1 .value != c2 .value :
10
- print (f"[w1'{ c1 .coordinate } ' != w2'{ c2 .coordinate } '] { c1 .value } != { c2 .value } " )
11
+ if case :
12
+ if c1 .value .lower () != c2 .value .lower ():
13
+ print (f"[w1'{ c1 .coordinate } ' != w2'{ c2 .coordinate } '] { c1 .value } != { c2 .value } " )
14
+ print ("WOWOWO" )
15
+
16
+ else :
17
+ if c1 .value != c2 .value :
18
+ print (f"[w1'{ c1 .coordinate } ' != w2'{ c2 .coordinate } '] { c1 .value } != { c2 .value } " )
11
19
12
- def compare_columns (ws1 : Worksheet , ws2 : Worksheet , col1 : str , col2 : str ):
20
+ def compare_columns (ws1 : Worksheet , ws2 : Worksheet , col1 : str , col2 : str , case : bool ):
13
21
for cell_1 , cell_2 in zip (ws1 [col1 ], ws2 [col2 ]):
14
- compare_cells (cell_1 , cell_2 )
22
+ compare_cells (cell_1 , cell_2 , case )
15
23
if len (ws1 [col1 ]) > len (ws2 [col2 ]):
16
24
print (f"[ws1'{ col1 } ' has more rows than ws2'{ col2 } ', skipping extra rows]\n " )
17
25
if len (ws1 [col1 ]) < len (ws2 [col2 ]):
18
26
print (f"[ws2'{ col2 } ' has more rows than ws1'{ col1 } ', skipping extra rows]\n " )
19
27
20
- def compare_entire_worksheets (ws1 : Worksheet , ws2 : Worksheet ):
28
+ def compare_entire_worksheets (ws1 : Worksheet , ws2 : Worksheet , case : bool ):
21
29
ws1_rows , ws2_rows = ws1 .max_row , ws2 .max_row
22
30
ws1_cols , ws2_cols = ws1 .max_column , ws2 .max_column
23
31
rows = min (ws1_rows , ws2_rows )
24
32
cols = min (ws1_cols , ws2_cols )
25
33
for i in range (1 , rows + 1 ):
26
34
for j in range (1 , cols + 1 ):
27
- compare_cells (ws1 .cell (i , j ), ws2 .cell (i , j ))
35
+ compare_cells (ws1 .cell (i , j ), ws2 .cell (i , j ), case )
28
36
if ws1_rows > ws2_rows :
29
37
print (f"[ws1'{ ws1 .title } ' has more rows than ws2'{ ws2 .title } ', skipping extra rows]" )
30
38
if ws1_rows < ws2_rows :
@@ -41,6 +49,7 @@ def compare_workbooks(
41
49
s2 : str = None ,
42
50
cols1 : str = None ,
43
51
cols2 : str = None ,
52
+ case : bool = False ,
44
53
):
45
54
ws1 : Worksheet = w1 [s1 ] if s1 else w1 .active
46
55
ws2 : Worksheet = w2 [s2 ] if s2 else w2 .active
@@ -51,11 +60,11 @@ def compare_workbooks(
51
60
return
52
61
else :
53
62
for c1 , c2 in zip ([x .strip () for x in cols1 .split ("," )], [x .strip () for x in cols2 .split ("," )]):
54
- compare_columns (ws1 , ws2 , c1 , c2 )
63
+ compare_columns (ws1 , ws2 , c1 , c2 , case )
55
64
56
- def main (w1 , w2 , s1 = None , s2 = None , cols1 = None , cols2 = None ):
65
+ def main (w1 , w2 , s1 = None , s2 = None , cols1 = None , cols2 = None , case = False ):
57
66
ws1 : Workbook = load_workbook (w1 )
58
67
ws2 : Workbook = load_workbook (w2 )
59
68
print ("Starting comparison...\n " )
60
- compare_workbooks (ws1 , ws2 , s1 = s1 , s2 = s2 , cols1 = cols1 , cols2 = cols2 )
69
+ compare_workbooks (ws1 , ws2 , s1 = s1 , s2 = s2 , cols1 = cols1 , cols2 = cols2 , case = case )
61
70
print ("\n Comparison finished!" )
0 commit comments