import sys, io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import openpyxl, importlib.util
from openpyxl.utils import get_column_letter
spec = importlib.util.spec_from_file_location('chk', r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\_검수체크.py')
chk = importlib.util.module_from_spec(spec); spec.loader.exec_module(chk)

PKG = r'D:\01.프로젝트\DB수집\작업파일\_제출패키지_2026-06-03\프리랜서1\충청남도_부여군.xlsx'
WRK = r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\충청남도\7.부여군\충청남도_부여군.xlsx'
BK  = PKG.replace('.xlsx','_backup_테두리보정전.xlsx')

n, issues = chk.inspect(PKG)
print(f'검수: {n}행 -> {"PASS" if not issues else "FAIL "+str(issues)}')

a = openpyxl.load_workbook(PKG).active
b = openpyxl.load_workbook(BK).active
vd = sum(1 for r in range(1,max(a.max_row,b.max_row)+1) for c in range(1,28) if a.cell(r,c).value!=b.cell(r,c).value)
hd = sum(1 for r in range(1,a.max_row+1) if a.row_dimensions[r].height!=b.row_dimensions[r].height)
print(f'값 변화 vs backup: {vd}  높이 변화: {hd}  (0이어야 정상)')
print(f'merge수: PKG={len(a.merged_cells.ranges)} backup={len(b.merged_cells.ranges)}')
print(f'max_row PKG={a.max_row} backup={b.max_row}')

# full border completeness for grid columns (exclude D=4,E=5,X=24)
def anyb(cell):
    bd=cell.border; return any([bd.left.style,bd.right.style,bd.top.style,bd.bottom.style])
gridcols=[2,3,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,25,26,27]
miss={}
for r in range(3,a.max_row+1):
    for c in gridcols:
        if not anyb(a.cell(r,c)):
            miss[get_column_letter(c)]=miss.get(get_column_letter(c),0)+1
print('grid columns with any borderless cell:', miss if miss else 'NONE')

# PKG == WRK identical borders check (spot)
w=openpyxl.load_workbook(WRK).active
def sig(ws,r,c):
    bd=ws.cell(r,c).border; return (bd.left.style,bd.right.style,bd.top.style,bd.bottom.style)
bd_diff=sum(1 for r in range(3,a.max_row+1) for c in range(2,28) if sig(a,r,c)!=sig(w,r,c))
print('PKG vs WRK border diffs:', bd_diff)
# anchor values preserved after F/G remerge (spot)
print('F136 anchor val:', a.cell(136,6).value, '| F526 anchor:', a.cell(526,6).value)
