import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import openpyxl
ws = openpyxl.load_workbook(r'D:\01.프로젝트\DB수집\작업파일\_제출패키지_2026-06-03\프리랜서1\충청남도_부여군.xlsx').active
merges=list(ws.merged_cells.ranges)
def eff(r,c):
    for m in merges:
        if m.min_row<=r<=m.max_row and m.min_col<=c<=m.max_col:
            return ws.cell(m.min_row,m.min_col).value
    return ws.cell(r,c).value
gmerges=sorted([m for m in merges if m.min_col==7 and m.min_row>=3], key=lambda x:x.min_row)
print(f'G merges: {len(gmerges)}')
# show first 6 G-merge regions in full (D..K)
for m in gmerges[:6]:
    a,z=m.min_row,m.max_row
    print(f'\n--- G{a}:{z} (val={ws.cell(a,7).value!r}) ---')
    for r in range(a, z+1):
        print(f'  r{r} D={eff(r,4)!r} E={eff(r,5)!r} F={eff(r,6)!r} G={ws.cell(r,7).value!r} H={ws.cell(r,8).value!r} I={ws.cell(r,9).value!r} K={ws.cell(r,11).value!r}')
# Are H/I/J ever populated under these G merges?
hpop=sum(1 for m in gmerges for r in range(m.min_row,m.max_row+1) if ws.cell(r,8).value not in (None,''))
print(f'\nG병합 구간 내 H(8) 값 있는 행 수: {hpop}')
# distribution of which D group the G merges fall in
from collections import Counter
dc=Counter(eff(m.min_row,4) for m in gmerges)
print('G병합 소속 대분류:', dict(dc))
