# -*- coding: utf-8 -*- # 탭 그룹별 '파일에 행으로 없는' 탭아이템(누락) 추출 import openpyxl, json, io, sys, re from urllib.parse import urljoin sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8') F=r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\충청남도\13.청양군\충청남도_청양군.xlsx' ws=openpyxl.load_workbook(F).active tabs=json.load(open('_cy3_tabs.json',encoding='utf-8')) def norm(s): return re.sub(r'\s+','',(s or '')).replace('·','').lower() def normurl(u): if not u: return '' u=u.split('#')[0].rstrip('/') return u.replace('https://','').replace('http://','').replace('www.cheongyang.go.kr','').replace('//','/') # 기존 행 라벨/URL 집합 labset=set(); urlset=set() for r in range(3,ws.max_row+1): for c in (5,6,7,8): v=ws.cell(r,c).value if v: labset.add(norm(v)) hl=ws.cell(r,11).hyperlink; u=hl.target if hl else ws.cell(r,11).value if u: urlset.add(normurl(u)) total_missing=0 out={} for rs,g in tabs.items(): r=int(rs); base=g['u'] miss=[] for t in g['tabs']: for it in t['items']: lab=it['t']; href=it['href'] absu=urljoin(base,href) if href else '' present = norm(lab) in labset or (absu and normurl(absu) in urlset) if not present: miss.append({'lab':lab,'href':href,'absu':absu,'blank':it['blank'],'cls':t['cls']}) if miss: # dedup within group by label seen=set(); m2=[] for x in miss: k=norm(x['lab']) if k in seen: continue seen.add(k); m2.append(x) out[rs]={'b':g['b'],'lab':g['lab'],'L':g['L'],'miss':m2} total_missing+=len(m2) print('r%d B%s %-18s 누락 %d: %s'%(r,g['b'],g['lab'][:18],len(m2),' | '.join('%s%s'%(x['lab'][:16],'(새창)'if x['blank']else'') for x in m2))) json.dump(out,open('_cy3_missing.json','w',encoding='utf-8'),ensure_ascii=False,indent=1) print('\n누락보유 그룹:',len(out),'| 총 누락아이템:',total_missing)