import sys, io, os, shutil, datetime
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import openpyxl
from openpyxl.styles import Alignment

P = r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\전북특별자치도\6.부안군\전북특별자치도_부안군.xlsx'
BK = P.replace('.xlsx','_backup_검수전.xlsx')
if not os.path.exists(BK): shutil.copy2(P,BK); print('backup ->', os.path.basename(BK))
wb=openpyxl.load_workbook(P); ws=wb.active

content=[r for r in range(3,ws.max_row+1) if ws.cell(r,2).value not in (None,'')
         or ws.cell(r,11).value not in (None,'') or any(ws.cell(r,c).value not in (None,'') for c in range(4,11))]

# (1) B순번 1..N
for i,r in enumerate(content,1): ws.cell(r,2).value=i

# (2) 행높이 15 (안전)
fh=0
for r in range(1,ws.max_row+1):
    if ws.row_dimensions[r].height not in (15.0,None): ws.row_dimensions[r].height=15.0; fh+=1

# (3) 정렬 통일
nonanchor=set()
for m in ws.merged_cells.ranges:
    for rr in range(m.min_row,m.max_row+1):
        for cc in range(m.min_col,m.max_col+1):
            if not (rr==m.min_row and cc==m.min_col): nonanchor.add((rr,cc))
CEN=Alignment(horizontal='center',vertical='center',wrap_text=True)
LEFT=Alignment(horizontal='left',vertical='center',wrap_text=False)
ac=0
for r in content:
    for c in (4,5,6,7):
        if (r,c) in nonanchor: continue
        ws.cell(r,c).alignment=CEN; ac+=1
    if (r,11) not in nonanchor: ws.cell(r,11).alignment=LEFT

# (4) Y/Z/AA (전북=프리랜서2)
d=datetime.datetime(2026,6,4); yc=0
for r in content:
    ws.cell(r,25).value='Y'; ws.cell(r,26).value=d; ws.cell(r,27).value='프리랜서2'; yc+=1

wb.save(P)
print(f'B순번 {len(content)}행 재부여 | 높이정규화 {fh} | 정렬 D/E/F/G {ac}셀+K | Y/Z/AA {yc}행(프2), saved')
