공공기관2 작업 중. _temp 몽타주(재생성가능)는 제외. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
28 lines
1.6 KiB
Python
28 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# B순번 1..N 재부번 (+옵션 헤더병합 재추가). 사용: _fix_brenum.py <no> [--hdr] [--apply]
|
|
import openpyxl, glob, os, sys, shutil
|
|
NO=int(sys.argv[1]); HDR='--hdr' in sys.argv; APPLY='--apply' in sys.argv
|
|
BASE=os.path.dirname(os.path.abspath(__file__))
|
|
d=[x for x in glob.glob(os.path.join(BASE,'%d.*'%NO)) if os.path.isdir(x)][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) and 'backup' not in p][0]
|
|
mt=os.path.getmtime(xp); wb=openpyxl.load_workbook(xp); ws=wb.active
|
|
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))]
|
|
print('%s: 데이터 %d행, B현재 %s..%s'%(os.path.basename(d),len(data),ws.cell(data[0],2).value,ws.cell(data[-1],2).value))
|
|
if HDR:
|
|
hm={str(m) for m in ws.merged_cells.ranges}
|
|
for h in ('B1:R1','S1:W1','Y1:AA1'):
|
|
if h not in hm: print(' 헤더병합 추가:',h)
|
|
if not APPLY: print('DRY'); sys.exit(0)
|
|
if HDR:
|
|
hm={str(m) for m in ws.merged_cells.ranges}
|
|
for h in ('B1:R1','S1:W1','Y1:AA1'):
|
|
if h not in hm:
|
|
try: ws.merge_cells(h)
|
|
except Exception as e: print(' 병합실패',h,e)
|
|
for i,r in enumerate(data): ws.cell(r,2).value=i+1
|
|
if os.path.getmtime(xp)!=mt: print('ABORT changed'); sys.exit(1)
|
|
shutil.copy(xp, os.path.join(d,'_backup',os.path.basename(xp).replace('.xlsx','_backup_검수보정전.xlsx')))
|
|
wb.save(xp)
|
|
bs=[ws.cell(r,2).value for r in data]
|
|
print('saved B연속%s 행수%d'%(bs==list(range(1,len(data)+1)),len(data)))
|