공공기관2 작업 중. _temp 몽타주(재생성가능)는 제외. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
119 lines
4.5 KiB
Python
119 lines
4.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
import openpyxl, json, re
|
|
from copy import copy
|
|
from openpyxl.styles import PatternFill
|
|
|
|
FN='국립호남권생물자원관.xlsx'
|
|
BLUE=PatternFill(fill_type='solid', fgColor='FFBDD7EE')
|
|
BASE='https://www.hnibr.re.kr'
|
|
tree=json.load(open('_hnibr_gnb.json',encoding='utf-8'))
|
|
|
|
# --- build GNB leaf rows: D=L1,E=L2,F=L3 or None ---
|
|
rows=[]
|
|
i=0
|
|
while i<len(tree):
|
|
n=tree[i]
|
|
if n['lvl']!=1: i+=1; continue
|
|
L1=n['t']; i+=1
|
|
while i<len(tree) and tree[i]['lvl']>=2:
|
|
if tree[i]['lvl']!=2: i+=1; continue
|
|
L2=tree[i]['t']; l2h=tree[i]['href']; i+=1
|
|
kids=[]
|
|
while i<len(tree) and tree[i]['lvl']==3: kids.append(tree[i]); i+=1
|
|
if kids:
|
|
for c in kids: rows.append({'D':L1,'E':L2,'F':c['t'],'href':c['href']})
|
|
else:
|
|
rows.append({'D':L1,'E':L2,'F':None,'href':l2h})
|
|
SKIP_E={'HOME','ENGLISH'}
|
|
rows=[r for r in rows if r['E'] not in SKIP_E and r['href'] not in ('/','/en')]
|
|
|
|
# --- existing L/M/N/O/S by menuID or url ---
|
|
wb=openpyxl.load_workbook(FN); ws=wb.active
|
|
def key(u):
|
|
m=re.search(r'/(M\d{9})/',str(u)); return m.group(1) if m else str(u).rstrip('/')
|
|
exist={}
|
|
for r in range(3,ws.max_row+1):
|
|
if ws.cell(r,2).value is None: continue
|
|
u=ws.cell(r,11).value
|
|
if not u: continue
|
|
k=key(u)
|
|
if k not in exist or not exist[k].get('L'):
|
|
hl=ws.cell(r,11).hyperlink
|
|
exist[k]=dict(L=ws.cell(r,12).value,M=ws.cell(r,13).value,N=ws.cell(r,14).value,
|
|
O=ws.cell(r,15).value,S=ws.cell(r,19).value,
|
|
hlink=hl.target if hl else u, url=u)
|
|
|
|
# --- assemble records ---
|
|
recs=[]
|
|
def addrec(D,E,F,url,LMNO=None,site=False,new=False):
|
|
k=key(url)
|
|
e=exist.get(k,{})
|
|
if site:
|
|
L,M,N,O,S='사이트',None,None,None,'외부링크'
|
|
else:
|
|
L=e.get('L'); M=e.get('M'); N=e.get('N'); O=e.get('O'); S=e.get('S')
|
|
recs.append(dict(D=D,E=E,F=F,url=url,hlink=e.get('hlink',url),L=L,M=M,N=N,O=O,S=S,new=new))
|
|
|
|
for r in rows:
|
|
url=r['href'] if r['href'].startswith('http') else BASE+r['href']
|
|
addrec(r['D'],r['E'],r['F'],url, site=(url.startswith('http') and 'hnibr.re.kr' not in url))
|
|
|
|
# 이용약관 (real page missed by GNB) -> 사이트메뉴
|
|
addrec('사이트메뉴','이용약관',None, BASE+'/ko/M000000531/html/view', new=True)
|
|
# 바로가기 (외부 featured links)
|
|
for E,url in [('통합예약시스템','https://resve.hnibr.re.kr'),('섬생물소재은행','https://ibis.hnibr.re.kr/bobic'),
|
|
('고하도 주변관광','https://www.mokpo.go.kr/tour'),('페이스북','https://www.facebook.com/hnibr.re.kr'),
|
|
('블로그','https://blog.naver.com/h_nibr'),('인스타그램','https://www.instagram.com/h_nibr/'),
|
|
('유튜브','https://www.youtube.com/@hnibr')]:
|
|
addrec('바로가기',E,None,url,site=True,new=True)
|
|
|
|
print('records:', len(recs))
|
|
|
|
# --- WRITE (fresh) ---
|
|
MAXC=27
|
|
# capture a template style per column from an existing data row (row3) and a merge-body row
|
|
tpl={c:copy(ws.cell(3,c)._style) for c in range(1,MAXC+1)}
|
|
# unmerge data merges
|
|
for mr in list(ws.merged_cells.ranges):
|
|
if mr.min_row>=3: ws.unmerge_cells(str(mr))
|
|
# clear all data rows
|
|
for r in range(3, ws.max_row+1):
|
|
for c in range(1,MAXC+1):
|
|
cell=ws.cell(r,c); cell.value=None; cell.hyperlink=None; cell.fill=PatternFill(fill_type=None)
|
|
|
|
SITE='국립호남권생물자원관'
|
|
n=len(recs)
|
|
for idx,rc in enumerate(recs):
|
|
r=3+idx
|
|
for c in range(1,MAXC+1): ws.cell(r,c)._style=copy(tpl[c])
|
|
ws.cell(r,2).value=idx+1
|
|
ws.cell(r,3).value=SITE
|
|
ws.cell(r,4).value=rc['D']; ws.cell(r,5).value=rc['E']; ws.cell(r,6).value=rc['F']
|
|
ws.cell(r,11).value=rc['url']
|
|
ws.cell(r,11).hyperlink=rc['hlink'] if rc['url'] else None
|
|
ws.cell(r,12).value=rc['L']; ws.cell(r,13).value=rc['M']; ws.cell(r,14).value=rc['N']
|
|
ws.cell(r,15).value=rc['O']; ws.cell(r,19).value=rc['S']
|
|
ws.row_dimensions[r].height=15
|
|
if rc['new']:
|
|
for c in range(4,16): ws.cell(r,c).fill=BLUE
|
|
|
|
# re-merge D, E, F
|
|
def remerge(col, scope):
|
|
r=3
|
|
while r<3+n:
|
|
v=ws.cell(r,col).value
|
|
if v is None or v=='': r+=1; continue
|
|
r2=r
|
|
while r2+1<3+n:
|
|
if ws.cell(r2+1,col).value!=v: break
|
|
ok=all(ws.cell(r2+1,sc).value==ws.cell(r,sc).value for sc in scope)
|
|
if not ok: break
|
|
r2+=1
|
|
if r2>r:
|
|
for rr in range(r+1,r2+1): ws.cell(rr,col).value=None
|
|
ws.merge_cells(start_row=r,start_column=col,end_row=r2,end_column=col)
|
|
r=r2+1
|
|
remerge(6,[4,5]); remerge(5,[4]); remerge(4,[])
|
|
wb.save(FN)
|
|
print('saved', n, 'rows')
|