import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import openpyxl
from openpyxl.utils import get_column_letter
ws = openpyxl.load_workbook(r'D:\01.프로젝트\DB수집\작업파일\_제출패키지_2026-06-03\프리랜서1\충청남도_부여군.xlsx').active
merges=list(ws.merged_cells.ranges)
def merge_of(r,c):
    for m in merges:
        if m.min_row<=r<=m.max_row and m.min_col<=c<=m.max_col: return m
    return None
def raw(r,c): return ws.cell(r,c).value
def eff(r,c):
    m=merge_of(r,c)
    return ws.cell(m.min_row,m.min_col).value if m else raw(r,c)
data=[r for r in range(3,ws.max_row+1) if ws.cell(r,2).value not in (None,'')]
dset=set(data)

print('=== 480~572 행 카테고리 + 병합상태 (D/E/F/G/H) ===')
def tag(r,c):
    m=merge_of(r,c)
    return f'<{m.min_row}:{m.max_row}>' if m else ''
for r in range(480, 573):
    if r not in dset:
        print(f'r{r} (B빈)'); continue
    parts=[]
    for c,nm in [(4,'D'),(5,'E'),(6,'F'),(7,'G'),(8,'H')]:
        v=raw(r,c)
        if v not in (None,'') or tag(r,c):
            parts.append(f'{nm}={v!r}{tag(r,c)}')
    print(f'r{r} ' + ' '.join(parts))

# (b) adjacent same-value cells NOT in the same merge (duplicated value -> should be merged)
print('\n=== 인접 같은값인데 병합 안 된 셀 (중복값형 미병합) D/E/F/G ===')
for c in (4,5,6,7):
    col=get_column_letter(c)
    hits=[]
    for i in range(len(data)-1):
        r1,r2=data[i],data[i+1]
        if r2!=r1+1: continue
        v1,v2=raw(r1,c),raw(r2,c)
        if v1 not in (None,'') and v1==v2:
            m1,m2=merge_of(r1,c),merge_of(r2,c)
            if not (m1 and m2 and m1==m2):
                # same parent?
                if tuple(eff(r1,cc) for cc in range(4,c))==tuple(eff(r2,cc) for cc in range(4,c)):
                    hits.append((r1,r2,v1))
    if hits:
        print(f'  {col}: {len(hits)}건', [(a,b,v) for a,b,v in hits][:20])
    else:
        print(f'  {col}: 없음')
