Repository navigation
Expand file tree
/
Copy pathreplace_with_formulas.py
More file actions
366 lines (315 loc) · 11.7 KB
/
Copy pathreplace_with_formulas.py
File metadata and controls
366 lines (315 loc) · 11.7 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import openpyxl
from openpyxl.utils import get_column_letter
INPUT_FILE = "1. Донецк ШР 01.01.26_план.xlsx"
OUTPUT_FILE = "1. Донецк ШР 01.01.26_план_formulas.xlsx"
SHEET_NAME = "ШР_26"
DATA_START_ROW = 27
wb = openpyxl.load_workbook(INPUT_FILE)
ws = wb[SHEET_NAME]
max_row = ws.max_row
def get_val(row, col):
return ws.cell(row=row, column=col).value
def is_rukovoditel_row(row):
d = get_val(row, 4)
return isinstance(d, str) and "Руководители" in d
def is_rabochie_row(row):
d = get_val(row, 4)
return isinstance(d, str) and d.strip() == "Рабочие"
def is_position_row(row):
d = get_val(row, 4)
if not isinstance(d, str) or d.strip() == "":
return False
if "Руководители" in d:
return False
if d.strip() == "Рабочие":
return False
return True
def is_subgroup_header(row):
c = get_val(row, 3)
d = get_val(row, 4)
return isinstance(c, str) and c.strip() != "" and (d is None or (isinstance(d, str) and d.strip() == ""))
def is_division_header(row):
b = get_val(row, 2)
d = get_val(row, 4)
return isinstance(b, str) and b.strip() != "" and (d is None or (isinstance(d, str) and d.strip() == ""))
def is_grand_total_row(row):
a = get_val(row, 1)
if not isinstance(a, str):
return False
a_upper = a.upper()
return "ИТОГ" in a_upper or "ВСЕГО" in a_upper
def is_formula(val):
return isinstance(val, str) and val.startswith("=")
def parse_subgroup_structure(start, end_limit):
"""Parse a sequence of sub-groups within a range.
Returns list of sub-groups and the row where parsing stopped."""
subgroups = []
i = start
while i <= end_limit:
if is_subgroup_header(i):
sg_header = i
sg_ruk = None
sg_ruk_pos = []
sg_rab = None
sg_rab_pos = []
j = i + 1
while j <= end_limit:
if is_rukovoditel_row(j) and sg_ruk is None:
sg_ruk = j
j += 1
continue
if is_rabochie_row(j) and sg_ruk is not None and sg_rab is None:
sg_rab = j
j += 1
continue
if sg_ruk is not None and sg_rab is None and is_position_row(j):
sg_ruk_pos.append(j)
j += 1
continue
if sg_rab is not None and is_position_row(j):
sg_rab_pos.append(j)
j += 1
continue
break
subgroups.append({
"header": sg_header,
"ruk": sg_ruk,
"ruk_pos": sg_ruk_pos,
"rab": sg_rab,
"rab_pos": sg_rab_pos,
})
i = j
elif is_rukovoditel_row(i):
# Standalone rukovoditel row (not under a sub-group header)
sg_ruk = i
sg_ruk_pos = []
j = i + 1
while j <= end_limit:
if is_rabochie_row(j):
sg_rab = j
j += 1
break
if is_position_row(j):
sg_ruk_pos.append(j)
j += 1
continue
break
sg_rab = None
sg_rab_pos = []
if j <= end_limit and is_rabochie_row(j - 1):
sg_rab = j - 1
while j <= end_limit:
if is_position_row(j):
sg_rab_pos.append(j)
j += 1
continue
break
subgroups.append({
"header": None,
"ruk": sg_ruk,
"ruk_pos": sg_ruk_pos,
"rab": sg_rab,
"rab_pos": sg_rab_pos,
})
i = j
else:
break
return subgroups, i
def process_subgroup(sg):
changes = {"k": 0, "l": 0}
ruk = sg["ruk"]
ruk_pos = sg["ruk_pos"]
rab = sg["rab"]
rab_pos = sg["rab_pos"]
header = sg["header"]
if ruk is not None and ruk_pos:
ps, pe = ruk_pos[0], ruk_pos[-1]
ws.cell(row=ruk, column=11).value = f"=SUM(K{ps}:K{pe})"
changes["k"] += 1
ws.cell(row=ruk, column=12).value = f"=SUM(L{ps}:L{pe})"
changes["l"] += 1
if rab is not None and rab_pos:
ps, pe = rab_pos[0], rab_pos[-1]
ws.cell(row=rab, column=11).value = f"=SUM(K{ps}:K{pe})"
changes["k"] += 1
ws.cell(row=rab, column=12).value = f"=SUM(L{ps}:L{pe})"
changes["l"] += 1
if header is not None and ruk is not None:
if rab is not None:
if not is_formula(get_val(header, 11)):
ws.cell(row=header, column=11).value = f"=K{ruk}+K{rab}"
changes["k"] += 1
if not is_formula(get_val(header, 12)):
ws.cell(row=header, column=12).value = f"=L{ruk}+L{rab}"
changes["l"] += 1
else:
if not is_formula(get_val(header, 11)):
ws.cell(row=header, column=11).value = f"=K{ruk}"
changes["k"] += 1
if not is_formula(get_val(header, 12)):
ws.cell(row=header, column=12).value = f"=L{ruk}"
changes["l"] += 1
for pr in ruk_pos:
if not is_formula(get_val(pr, 12)):
ws.cell(row=pr, column=12).value = f"=I{pr}*K{pr}"
changes["l"] += 1
for pr in rab_pos:
if not is_formula(get_val(pr, 12)):
ws.cell(row=pr, column=12).value = f"=I{pr}*K{pr}"
changes["l"] += 1
return changes
def identify_groups():
groups = []
i = DATA_START_ROW
while i <= max_row:
if is_grand_total_row(i):
break
if is_division_header(i):
div_header = i
ruk_row = None
ruk_pos_rows = []
rabochie_row = None
rab_pos_rows = []
subgroups = []
j = i + 1
while j <= max_row:
if is_rukovoditel_row(j) and ruk_row is None:
ruk_row = j
j += 1
continue
if is_rabochie_row(j) and ruk_row is not None and rabochie_row is None:
rabochie_row = j
j += 1
continue
if ruk_row is not None and rabochie_row is None and is_position_row(j):
ruk_pos_rows.append(j)
j += 1
continue
if ruk_row is not None and is_position_row(j):
rab_pos_rows.append(j)
j += 1
continue
if ruk_row is not None and is_subgroup_header(j):
# Found sub-groups after the main structure
subgroups, j = parse_subgroup_structure(j, max_row)
continue
break
groups.append({
"div_header": div_header,
"ruk_row": ruk_row,
"ruk_pos_rows": ruk_pos_rows,
"rabochie_row": rabochie_row,
"rab_pos_rows": rab_pos_rows,
"subgroups": subgroups,
})
i = j
else:
i += 1
return groups
def find_grand_total_row():
for r in range(DATA_START_ROW, max_row + 1):
if is_grand_total_row(r):
return r
return None
groups = identify_groups()
grand_total_row = find_grand_total_row()
print(f"Found {len(groups)} division groups")
print(f"Grand total row: {grand_total_row}")
sg_count = sum(len(g["subgroups"]) for g in groups)
print(f"Total sub-groups: {sg_count}")
for idx, g in enumerate(groups):
dh = g["div_header"]
rr = g["ruk_row"]
rpr = g["ruk_pos_rows"]
rb = g["rabochie_row"]
rbp = g["rab_pos_rows"]
sgs = g["subgroups"]
dh_name = str(get_val(dh, 2))[:40]
info = f"div_header={dh} ('{dh_name}'), ruk_row={rr}, ruk_pos={len(rpr)}"
if rb:
info += f", rabochie_row={rb}, rab_pos={len(rbp)}"
if sgs:
info += f", subgroups={len(sgs)}"
print(f" Group {idx}: {info}")
changes_k = 0
changes_l = 0
for g in groups:
ruk_row = g["ruk_row"]
ruk_pos_rows = g["ruk_pos_rows"]
rabochie_row = g["rabochie_row"]
rab_pos_rows = g["rab_pos_rows"]
div_header = g["div_header"]
subgroups = g["subgroups"]
# --- Rukovoditel row ---
if ruk_row is not None and ruk_pos_rows:
ps, pe = ruk_pos_rows[0], ruk_pos_rows[-1]
ws.cell(row=ruk_row, column=11).value = f"=SUM(K{ps}:K{pe})"
changes_k += 1
ws.cell(row=ruk_row, column=12).value = f"=SUM(L{ps}:L{pe})"
changes_l += 1
# --- Rabochie row ---
if rabochie_row is not None:
# Find the last position row (either in rab_pos_rows or in sub-groups)
all_rab_positions = list(rab_pos_rows)
for sg in subgroups:
all_rab_positions.extend(sg.get("ruk_pos", []))
all_rab_positions.extend(sg.get("rab_pos", []))
if all_rab_positions:
ps, pe = min(all_rab_positions), max(all_rab_positions)
ws.cell(row=rabochie_row, column=11).value = f"=SUM(K{ps}:K{pe})"
changes_k += 1
ws.cell(row=rabochie_row, column=12).value = f"=SUM(L{ps}:L{pe})"
changes_l += 1
# --- Sub-groups ---
for sg in subgroups:
sg_changes = process_subgroup(sg)
changes_k += sg_changes["k"]
changes_l += sg_changes["l"]
# --- Division header ---
if ruk_row is not None:
existing_k = get_val(div_header, 11)
existing_l = get_val(div_header, 12)
if rabochie_row is not None:
if not is_formula(existing_k):
ws.cell(row=div_header, column=11).value = f"=K{ruk_row}+K{rabochie_row}"
changes_k += 1
if not is_formula(existing_l):
ws.cell(row=div_header, column=12).value = f"=L{ruk_row}+L{rabochie_row}"
changes_l += 1
else:
if not is_formula(existing_k):
ws.cell(row=div_header, column=11).value = f"=K{ruk_row}"
changes_k += 1
if not is_formula(existing_l):
ws.cell(row=div_header, column=12).value = f"=L{ruk_row}"
changes_l += 1
# --- Position rows under Rukovoditel ---
for pr in ruk_pos_rows:
if not is_formula(get_val(pr, 12)):
ws.cell(row=pr, column=12).value = f"=I{pr}*K{pr}"
changes_l += 1
# --- Position rows under Rabochie ---
for pr in rab_pos_rows:
if not is_formula(get_val(pr, 12)):
ws.cell(row=pr, column=12).value = f"=I{pr}*K{pr}"
changes_l += 1
# --- Grand total row ---
if grand_total_row is not None:
div_headers = [g["div_header"] for g in groups if g["div_header"] is not None]
if div_headers:
if not is_formula(get_val(grand_total_row, 10)):
j_parts = "+".join([f"J{dh}" for dh in div_headers])
ws.cell(row=grand_total_row, column=10).value = f"={j_parts}"
if not is_formula(get_val(grand_total_row, 12)):
l_parts = "+".join([f"L{dh}" for dh in div_headers])
ws.cell(row=grand_total_row, column=12).value = f"={l_parts}"
changes_l += 1
ruk_rows = [g["ruk_row"] for g in groups if g["ruk_row"] is not None]
if ruk_rows:
if not is_formula(get_val(grand_total_row, 11)):
k_parts = "+".join([f"K{rr}" for rr in ruk_rows])
ws.cell(row=grand_total_row, column=11).value = f"={k_parts}"
changes_k += 1
print(f"\nChanges made: K={changes_k}, L={changes_l}")
wb.save(OUTPUT_FILE)
print(f"\nSaved to: {OUTPUT_FILE}")