#!/usr/bin/python
# -*- coding: utf-8 -*-

# 每個月的助手列表

FILENAME = '/Public/2012/09/2012年9月各月份外科手術主治及住院醫師資料.xls'
FILENAME = '/Public/2013/03/10203_[MonlyReport_M387]_T0_前月份外科手術主治及住院醫師資料.xls'
FILENAME = u'/shares/Public/2020/外科手術主治及住院醫師資料/10904_[MonlyReport_M387]_T0_前月份外科手術主治及住院醫師資料.xls'

from datetime import *
from xlrd import *

#from tempfile import TemporaryFile
from xlwt import Workbook


import os
import re


def Tokenization(s):
    s = s.replace(u'、', u' ')
    s = s.replace(u'，', u' ')
    s = s.replace(u'.', u' ')
    tlist = re.split('[a-zA-Z0-9_ ,;/\(\)!]+', s)
    b = []
    for t in tlist:
        if t:
            b.append(t)
    print (b)
    return b
    
def Sum(s):
    adict = {}
    print (s.name)
    for row in range(s.nrows):
        try:
            code = s.cell(row,0).value
            date_value = xldate_as_tuple(s.cell(row,1).value, wb.datemode)
            d = datetime(*date_value)
            physician = s.cell(row,2).value
            assistants = s.cell(row,3).value
        except:
            continue
        
        for a in Tokenization(assistants):
            try:
                adict[a] += 1
            except:
                adict[a] = 1

    book = Workbook()
    sheet1 = book.add_sheet('Sheet 1')
    row = 0                
    for k in sorted(adict.keys()):
        if adict[k] == 1:
            continue
        sheet1.write(row,0,k)
        sheet1.write(row,1,adict[k])
        row+=1
        print (' %s,%s' % (k, adict[k]))
        
    print ('\n\n\n')
    out = '%s/%s.xls' % (os.path.dirname(FILENAME), s.name)
    book.save(out)
        

wb = open_workbook(FILENAME)
for s in wb.sheets():
    Sum(s)
        
#    exit()
