forked from juko-jam/auto_unit_setter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergin_identical_cells.py
55 lines (36 loc) · 1.25 KB
/
mergin_identical_cells.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
from openpyxl import load_workbook
# target_excel_file = 'last_format.xlsx'
# sheet_name = 'new_format_excel'
target_excel_file = "_primary.xlsx"
sheet_name = "primary"
workbook = load_workbook(filename=target_excel_file)
list_of_merge = []
last_value = None
# current_row = 0
# current_column = 0
ws = workbook[sheet_name]
row_count = ws.max_row
column_count = ws.max_column
start = 1
count = 0
for row in range(1, row_count + 1):
last_value = ws.cell(row= row,column=1).value
for column in range(2, column_count + 1):
if(last_value == ws.cell(row= row, column=column).value):
count += 1
else:
if(count != 0):
list_of_merge.append((row,start,count))
count = 0
start = column
last_value = ws.cell(row=row, column = column).value
if(count != 0):
list_of_merge.append((row,start,count))
start = 1
count = 0
for item in list_of_merge:
row, start_column, count = item
ws.merge_cells(start_row=row,end_row=row, start_column=start_column, end_column= start_column + count)
workbook.save(filename=target_excel_file)
#ws.merge_cells(start_row=1,start_column=1,end_row=1,end_column=2)
#workbook.save(filename=target_excel_file)