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)
cross=0
for m in gmerges:
    fvals={eff(r,6) for r in range(m.min_row,m.max_row+1)}
    evals={eff(r,5) for r in range(m.min_row,m.max_row+1)}
    if len(fvals)>1 or len(evals)>1:
        cross+=1; print('CROSS', m, 'F=',fvals,'E=',evals)
# H/I/J merges?
hij=[str(m) for m in merges if m.min_col>=8 and m.min_row>=3]
# rows under G-merges always have own H value?
missingH=[]
for m in gmerges:
    for r in range(m.min_row,m.max_row+1):
        if ws.cell(r,8).value in (None,''): missingH.append(r)
print(f'G병합 {len(gmerges)}개  F/E경계 넘는것 {cross}개  H/I/J병합 {len(hij)}개  G병합내 H빈칸 {len(missingH)}행')
# which F groups (읍면 etc) contain G merges
fset=sorted({eff(m.min_row,6) for m in gmerges})
print('G병합 포함 F그룹:', fset)
print('E그룹:', sorted({eff(m.min_row,5) for m in gmerges}))
