공공기관2 작업 중. _temp 몽타주(재생성가능)는 제외. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
64 lines
3.7 KiB
Python
64 lines
3.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
import openpyxl, glob, os, re, sys, shutil
|
|
from copy import copy
|
|
from openpyxl.styles import PatternFill
|
|
BASE=os.path.dirname(os.path.abspath(__file__)); MAXC=27
|
|
NO=int(sys.argv[1]); APPLY='--apply' in sys.argv
|
|
JUNK_EXACT={'전체메뉴 열기','전체메뉴 닫기','메뉴 열기','메뉴 닫기','전체 메뉴','인기검색어','검색창 닫기','검색창 열기','SCROLL DOWN','TOP','퀵메뉴','컨텐츠영역 바로가기','로그인페이지 바로가기','이달의행사'}
|
|
d=[x for x in glob.glob(os.path.join(BASE,'*')) if os.path.isdir(x) and os.path.basename(x).startswith(str(NO)+'.')][0]
|
|
xp=[p for p in glob.glob(os.path.join(d,'*.xlsx')) if not os.path.basename(p).startswith(('_','~')) and 'conflict' not in os.path.basename(p)][0]
|
|
mt=os.path.getmtime(xp); wb=openpyxl.load_workbook(xp); ws=wb.active
|
|
def eff(r,c):
|
|
v=ws.cell(r,c).value
|
|
if v is not None: return v
|
|
for mr in ws.merged_cells.ranges:
|
|
if mr.min_col==c and mr.min_row<=r<=mr.max_row: return ws.cell(mr.min_row,c).value
|
|
return None
|
|
data=[r for r in range(3,ws.max_row+1) if any(ws.cell(r,c).value not in (None,'') for c in (2,4,5,6,7,11))]
|
|
delset=set()
|
|
for r in data:
|
|
D=str(eff(r,4) or '').strip(); E=str(eff(r,5) or ''); F=str(ws.cell(r,6).value or ''); u=str(ws.cell(r,11).value or '')
|
|
noreal = (not u) or u.startswith('#') or 'javascript' in u.lower()
|
|
if 'gb.go.kr' in u: delset.add(r) # 경상북도 템플릿 오염
|
|
elif re.match(r'^\d+$', D) and not E and not F: delset.add(r) # 숫자대분류+하위빈=페이지네이션/달력 (E/F있으면 실콘텐츠 보존)
|
|
elif noreal and (D in JUNK_EXACT or '바로가기' in D or '더보기' in (E+F)): delset.add(r) # URL없는 UI버튼
|
|
print('site %d 정크삭제 %d행'%(NO,len(delset)))
|
|
for r in sorted(delset)[:12]: print(' r%d D=%s E=%s F=%s | %s'%(r,eff(r,4),eff(r,5),ws.cell(r,6).value,str(ws.cell(r,11).value)[:35]))
|
|
if not APPLY:
|
|
print('DRY'); sys.exit(0)
|
|
keep=[r for r in data if r not in delset]
|
|
snap={(r,c):copy(ws.cell(r,c)._style) for r in data for c in range(1,MAXC+1)}
|
|
recs=[]
|
|
for r in keep:
|
|
rec={'src':r,'D':eff(r,4),'E':eff(r,5),'F':ws.cell(r,6).value,'url':ws.cell(r,11).value}
|
|
for c in range(7,MAXC+1): rec[c]=ws.cell(r,c).value
|
|
hl=ws.cell(r,11).hyperlink; rec['hl']=hl.target if hl else rec['url']; recs.append(rec)
|
|
for mr in list(ws.merged_cells.ranges):
|
|
if mr.min_row>=3: ws.unmerge_cells(str(mr))
|
|
n=len(recs)
|
|
for i,rc in enumerate(recs):
|
|
r=3+i; src=rc['src']
|
|
for c in range(1,MAXC+1): ws.cell(r,c)._style=copy(snap[(src,c)])
|
|
ws.cell(r,2).value=i+1; ws.cell(r,3).value=ws.cell(src,3).value
|
|
ws.cell(r,4).value=rc['D']; ws.cell(r,5).value=rc['E']; ws.cell(r,6).value=rc['F']
|
|
for c in range(7,MAXC+1): ws.cell(r,c).value=rc.get(c)
|
|
kc=ws.cell(r,11); kc.value=rc['url']; kc.hyperlink=rc['hl'] if rc['url'] else None
|
|
for r in range(3+n,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)
|
|
def rem(col,sc):
|
|
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 and ws.cell(r2+1,col).value==v and all(ws.cell(r2+1,s).value==ws.cell(r,s).value for s in sc): 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
|
|
rem(7,[4,5,6]); rem(6,[4,5]); rem(5,[4]); rem(4,[])
|
|
if os.path.getmtime(xp)!=mt: print('ABORT'); sys.exit(1)
|
|
shutil.copy(xp, os.path.join(d,'_backup',os.path.basename(xp).replace('.xlsx','_backup_정크정리전.xlsx')))
|
|
wb.save(xp); print('saved %d행'%n)
|