# -*- coding: utf-8 -*-
import openpyxl, json, shutil
from copy import copy
from openpyxl.styles import PatternFill
XP='한국중부발전.xlsx'
BLUE=PatternFill('solid',fgColor='FFBDD7EE',bgColor='FFBDD7EE')
res=json.load(open('inpage.json',encoding='utf-8'))
wb=openpyxl.load_workbook(XP);ws=wb.active
shutil.copy(XP,XP.replace('.xlsx','_backup_인페이지탭M전.xlsx'))
ch=[]
for r_s,v in sorted(res.items(),key=lambda x:int(x[0])):
    r=int(r_s); n=len(v['tabs'])
    # safety: only update 페이지 rows, row>88
    if r<=88: continue
    if ws.cell(r,12).value!='페이지': 
        print('SKIP non-page r%d %s'%(r,ws.cell(r,12).value)); continue
    old=ws.cell(r,13).value
    ws.cell(r,13).value=n
    ws.cell(r,13).fill=copy(BLUE)
    ch.append((r,v['b'],v['leaf'],old,n,v['tabs']))
wb.save(XP)
for r,b,leaf,old,n,tabs in ch:
    print('r%d b%s %s : M %s->%d  %s'%(r,b,leaf,old,n,tabs))
print('변경',len(ch),'행')
