# -*- coding: utf-8 -*-
"""보령시 210행~끝: E병합 그룹별 F자식 형태/수량 스캔. 합치기 대상 판정."""
import openpyxl
XLSX = r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\충청남도\6.보령시\충청남도_보령시.xlsx'
ws = openpyxl.load_workbook(XLSX).active

last = 3
for r in range(3, ws.max_row + 1):
    if ws.cell(r, 11).value:
        last = r
print('마지막 데이터행', last)

# E 병합 범위(>=210)
emerges = {}
for mr in ws.merged_cells.ranges:
    if mr.min_col == 5 and mr.max_row >= 210:
        emerges[mr.min_row] = (mr.min_row, mr.max_row, ws.cell(mr.min_row, 5).value)

# 210부터 행 훑기
r = 210
elig = noelig = 0
while r <= last:
    e = ws.cell(r, 5).value
    # E 병합?
    rng = None
    for mr in ws.merged_cells.ranges:
        if mr.min_col == 5 and mr.min_row <= r <= mr.max_row:
            rng = (mr.min_row, mr.max_row); break
    if rng and rng[1] > rng[0]:
        r1, r2 = rng
        elabel = ws.cell(r1, 5).value
        dlabel = ws.cell(r1, 4).value
        ls = [ws.cell(x, 12).value for x in range(r1, r2 + 1)]
        ms = [ws.cell(x, 13).value for x in range(r1, r2 + 1)]
        fs = [ws.cell(x, 6).value for x in range(r1, r2 + 1)]
        allpage = all((l in ('페이지', '사이트')) for l in ls)
        kinds = set(ls)
        tag = 'O합치기' if allpage else 'X(게시판섞임)'
        if allpage: elig += 1
        else: noelig += 1
        try:
            ssum = sum(int(m) for m in ms if m is not None)
        except Exception:
            ssum = '?'
        print('E[%d~%d] %s > %s  (%d행) L=%s M합=%s  %s' % (r1, r2, dlabel, elabel, r2 - r1 + 1, kinds, ssum, tag))
        if not allpage:
            for x in range(r1, r2 + 1):
                print('      - %s L=%s M=%s' % (ws.cell(x, 6).value, ws.cell(x, 12).value, ws.cell(x, 13).value))
        r = r2 + 1
    else:
        # 단일행(E 비병합) 또는 E없음
        r += 1
print('\n합치기대상 E그룹:', elig, '/ 제외(게시판섞임):', noelig)
