# -*- coding: utf-8 -*-
import sys, io, openpyxl, collections
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
wb=openpyxl.load_workbook(r'3.한국의약품안전관리원/한국의약품안전관리원.xlsx'); ws=wb.active
# find last data row by B
last=2
for r in range(3,ws.max_row+1):
    if ws.cell(r,2).value is not None: last=r
N=last-2
print('데이터행수:', N, '(rows 3..',last,')')
# B 연속
bs=[ws.cell(r,2).value for r in range(3,last+1)]
print('B 1..N 연속:', bs==list(range(1,N+1)))
Lc=collections.Counter(); Nc=collections.Counter(); kogl=0
hdr=0; pageM=collections.Counter()
issues=[]
D1={'의약품 이상사례보고','의약품 안전관리','DUR 정보','부작용 피해구제','임상시험안전지원','알림마당','교육·홍보','정보공개','기관소개'}
for r in range(3,last+1):
    D=ws.cell(r,4).value; E=ws.cell(r,5).value; F=ws.cell(r,6).value
    L=ws.cell(r,12).value; M=ws.cell(r,13).value; Nn=ws.cell(r,14).value
    O=ws.cell(r,15).value; K=ws.cell(r,11).value
    isHdr = (D in D1 and (E in(None,'')) and (F in(None,'')) and (K in(None,'')))
    if isHdr:
        hdr+=1
        if L or M or Nn or O: issues.append(f'r{r} d1헤더에 LMNO값')
        continue
    Lc[L]+=1; Nc[Nn]+=1
    if O and O!='미부착': kogl+=1
    if L=='사이트':
        if M or Nn or O: issues.append(f'r{r} 사이트인데 MNO채워짐 M={M} N={Nn} O={O}')
    elif L=='페이지':
        pageM[M]+=1
    elif L=='게시판':
        if not isinstance(M,int): issues.append(f'r{r} 게시판 M비숫자 {M}')
# 병합 겹침/부모경계 검사
from openpyxl.utils import range_boundaries
ranges=[str(m) for m in ws.merged_cells.ranges]
print('대분류헤더행:',hdr)
print('L:',dict(Lc))
print('페이지 M분포:',dict(pageM))
print('KOGL부착:',kogl)
print('N:',dict(Nc))
print('이슈:', issues if issues else '없음')
