import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import openpyxl
P = r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\충청남도\10.아산시\충청남도_아산시.xlsx'
BK = P.replace('.xlsx','_backup_openInfo탭+S삭제전.xlsx')
wb=openpyxl.load_workbook(P); ws=wb.active
bk=openpyxl.load_workbook(BK).active

print('max_row', ws.max_row)
print('\n=== rows 36-48 (B,D,E,F,G,K,L,M,N,O) ===')
for r in range(36,49):
    print(f'r{r} B={ws[f"B{r}"].value!r} D={ws[f"D{r}"].value!r} E={ws[f"E{r}"].value!r} F={ws[f"F{r}"].value!r} G={ws[f"G{r}"].value!r}')
    print(f'      K={ws[f"K{r}"].value!r} hl={ws[f"K{r}"].hyperlink.target if ws[f"K{r}"].hyperlink else None} L={ws[f"L{r}"].value!r} M={ws[f"M{r}"].value!r} N={ws[f"N{r}"].value!r} O={ws[f"O{r}"].value!r}')

print('\n=== merges around 37-66 ===')
for m in sorted(ws.merged_cells.ranges,key=lambda x:(x.min_row,x.min_col)):
    if m.min_row<=66 and m.max_row>=37:
        print(m)

print('\n=== K value==hyperlink check (all rows) ===')
bad=0
for r in range(3,ws.max_row+1):
    c=ws[f'K{r}']
    if c.value and str(c.value).startswith('http'):
        if not c.hyperlink or c.hyperlink.target!=c.value:
            bad+=1; print('  MISMATCH r',r, c.value, '!=', c.hyperlink.target if c.hyperlink else None)
print('K mismatches:', bad)

print('\n=== S 외부링크 remaining ===')
rem=[r for r in range(3,ws.max_row+1) if str(ws[f'S{r}'].value).strip()=='외부링크']
print('remaining:', rem)

print('\n=== rows 1-36 identical to backup? (B..AA) ===')
diff=0
for r in range(1,37):
    for c in range(1,28):
        a=ws.cell(r,c).value; b=bk.cell(r,c).value
        if a!=b:
            diff+=1; print(f'  DIFF r{r}c{c}: {b!r} -> {a!r}')
print('1-36 diffs:', diff)

print('\n=== old rows 38..471 preserved as 47..480? spot + full value check ===')
mism=0
for old in range(38,472):
    new=old+9
    for c in range(3,28):   # C..AA (skip B which is renumbered)
        a=bk.cell(old,c).value; b=ws.cell(new,c).value
        if a!=b:
            mism+=1
            if mism<=15: print(f'  shift DIFF old r{old}c{c}={a!r} new r{new}={b!r}')
print('shift content diffs (C..AA):', mism)

print('\n=== row 25 장영실 untouched ===')
print('r25 F=',ws['F25'].value,'O=',ws['O25'].value,'P=',ws['P25'].value,'Q=',ws['Q25'].value)
