import openpyxl, io, json
from openpyxl.utils import get_column_letter
wb=openpyxl.load_workbook('한국지역난방공사.xlsx'); ws=wb.active
print('maxrow',ws.max_row)
# B continuity
bs=[ws.cell(r,2).value for r in range(3,ws.max_row+1)]
print('B first/last',bs[0],bs[-1],'len',len(bs),'gaps',[i+3 for i,b in enumerate(bs) if b!=i+1][:10])
# K hyperlink check: value vs target mismatch
mis=0; nolink=0
for r in range(3,ws.max_row+1):
    k=ws.cell(r,11); 
    if k.value:
        if not k.hyperlink: nolink+=1
        elif k.hyperlink.target!=k.value: mis+=1
print('K value-no-link',nolink,'K mismatch',mis)
# merge overlaps
ms=list(ws.merged_cells.ranges)
print('merges',len(ms))
# sites with MNOPQ filled (should be blank)
bad=0
for r in range(3,ws.max_row+1):
    if ws.cell(r,12).value=='사이트':
        if any(ws.cell(r,c).value not in (None,'') for c in (13,14,15,16,17)): bad+=1
print('site rows with MNOPQ filled',bad)
# pages with M!=1
pm=[r for r in range(3,ws.max_row+1) if ws.cell(r,12).value=='페이지' and ws.cell(r,13).value!=1]
print('page M!=1',pm[:10])
# dump full to json for reading
out=[]
def eff(r,c):
    v=ws.cell(r,c).value
    if v is not None: return v
    for m in ms:
        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 None
for r in range(3,ws.max_row+1):
    out.append({'r':r,'B':ws.cell(r,2).value,
                'D':eff(r,4),'E':eff(r,5),'F':eff(r,6),'G':eff(r,7),'H':eff(r,8),
                'K':ws.cell(r,11).value,'L':ws.cell(r,12).value,'M':ws.cell(r,13).value,'N':ws.cell(r,14).value,'O':ws.cell(r,15).value})
io.open('_after.json','w',encoding='utf-8').write(json.dumps(out,ensure_ascii=False,indent=0))
print('wrote _after.json')
