131 lines
5.5 KiB
Python
131 lines
5.5 KiB
Python
|
from datetime import datetime
|
||
|
import xlrd
|
||
|
import xmltodict
|
||
|
|
||
|
|
||
|
DAYS={
|
||
|
'th_TH': ['จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์', 'อาทิตย์'],
|
||
|
'th_TH2': ['จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์', 'อาทิตย์'],
|
||
|
'en_US': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
||
|
}
|
||
|
|
||
|
MONTHS={
|
||
|
'th_TH': [None, 'มกราคม', 'กุมภาพันธ์', 'มีนาคม', 'เมษายน', 'พฤษภาคม', 'มิถุนายน', 'กรกฎาคม', 'สิงหาคม', 'กันยายน', 'ตุลาคม', 'พฤศจิกายน', 'ธันวาคม'],
|
||
|
'th_TH2': [None, 'ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.'],
|
||
|
'en_US': [None, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
|
||
|
}
|
||
|
|
||
|
PATIENT_TYPE={
|
||
|
"sc":"ปกส.",
|
||
|
"uc":"UC.",
|
||
|
"others": "จ่ายเอง",
|
||
|
}
|
||
|
|
||
|
STAFF_TYPE={
|
||
|
'doctor': 'แพทย์',
|
||
|
'nurse': 'พยาบาล',
|
||
|
'staff': 'ทั่วไป',
|
||
|
}
|
||
|
|
||
|
TOPICS={
|
||
|
'topic1': {'name': 'จำนวนครั้งการทำ Hemodialysis', 'unit': 'ครั้ง'},
|
||
|
'topic2': {'name': 'จำนวนผู้ป่วยยกมาจากเดือน', 'unit': 'คน'},
|
||
|
'topic3': {'name': 'จำนวนผู้ป่วยรับใหม่เดือน', 'unit': 'คน'},
|
||
|
'topic4': {'name': 'จำนวนผู้ป่วยจำหน่ายเดือน', 'unit': 'คน'},
|
||
|
'topic5': {'name': 'จำนวนผู้ป่วยยกไปเดือน', 'unit': 'คน'},
|
||
|
'topic6': {'name': 'จำนวนผู้ป่วยเบิก ปกส.', 'unit': 'คน'},
|
||
|
'topic7': {'name': 'จำนวนผู้ป่วยเบิก สปกส.', 'unit': 'คน'},
|
||
|
'topic8': {'name': 'จำนวนผู้ป่วยจ่ายเอง', 'unit': 'คน'},
|
||
|
}
|
||
|
|
||
|
def read_excel(fpath=None,show_datetime=False):
|
||
|
data={}
|
||
|
if fpath:
|
||
|
suffix=fpath.split(".")[-1]
|
||
|
if suffix not in ('xls', 'xlsx'):
|
||
|
raise Exception("ERROR : please should file xls or xlsx")
|
||
|
wb=xlrd.open_workbook(fpath)
|
||
|
sheet=wb.sheet_by_name("Sheet1")
|
||
|
keys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)]
|
||
|
data=[]
|
||
|
for row_index in range(1, sheet.nrows):
|
||
|
d={}
|
||
|
for col_index in range(sheet.ncols):
|
||
|
ctype=sheet.cell(row_index,col_index).ctype
|
||
|
if ctype==3:
|
||
|
value=sheet.cell(row_index, col_index).value
|
||
|
year, month, day, hour, minute, second = xlrd.xldate_as_tuple(value,wb.datemode)
|
||
|
value=datetime(year, month, day, hour, minute,second)
|
||
|
if show_datetime:
|
||
|
value=value.strftime("%Y-%m-%d %H:%M:%S")
|
||
|
else:
|
||
|
value=value.strftime("%Y-%m-%d")
|
||
|
else:
|
||
|
value=sheet.cell(row_index, col_index).value
|
||
|
d.update({(keys[col_index] or "").lower():value})
|
||
|
data.append(d)
|
||
|
return data
|
||
|
|
||
|
def read_xml(fpath=None,node="HDBills"):
|
||
|
root_name="STMSTM"
|
||
|
child_name=node
|
||
|
data={}
|
||
|
if not child_name:
|
||
|
return data
|
||
|
if fpath:
|
||
|
suffix=fpath.split(".")[-1]
|
||
|
if suffix not in ('xml'):
|
||
|
raise Exception("ERROR : please should file xml")
|
||
|
data=xmltodict.parse(open(fpath,"r").read())
|
||
|
root=data.get(root_name)
|
||
|
lines=[]
|
||
|
if root:
|
||
|
child=root.get(child_name)
|
||
|
if not child:
|
||
|
return {}
|
||
|
for k, v in child.items():
|
||
|
collections=v
|
||
|
for collection in collections:
|
||
|
if isinstance(collection,dict):
|
||
|
line={}
|
||
|
for i, j in collection.items():
|
||
|
key=(i or "").lower()
|
||
|
line[key]=j
|
||
|
lines.append(line)
|
||
|
return lines
|
||
|
|
||
|
def date2thai(date, format='%(BY)s-%(m)s-%(d)s', lang='th_TH'):
|
||
|
'''
|
||
|
>>> date2thai('2011-12-31', lang='th_TH')
|
||
|
'2554-12-31'
|
||
|
>>> date2thai('2011-12-31', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='en_US')
|
||
|
'Saturday 31 December, 54'
|
||
|
>>> print date2thai('2011-12-31', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='th_TH')
|
||
|
เสาร์ 31 ธันวาคม, 54
|
||
|
>>> date2thai('2000-06-08', lang='th_TH')
|
||
|
'2543-06-08'
|
||
|
>>> date2thai('2000-06-08', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='en_US')
|
||
|
'Thursday 08 June, 43'
|
||
|
>>> print date2thai('2000-06-08', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='th_TH')
|
||
|
พฤหัสบดี 08 มิถุนายน, 43
|
||
|
'''
|
||
|
|
||
|
if not date or not date.count('-') == 2:
|
||
|
return ''
|
||
|
|
||
|
year, month, day = date.split('-')
|
||
|
|
||
|
#dow = DateTime.Date(int(year), int(month), int(day)).day_of_week
|
||
|
dow = datetime(int(year),int(month),int(day)).weekday()
|
||
|
return format % { 'BY': int(year) + 543
|
||
|
, 'By': int(year[2:]) + 43
|
||
|
, 'Tm': MONTHS[lang][int(month)]
|
||
|
, 'Td': DAYS[lang][dow]
|
||
|
, 'm': month
|
||
|
, 'd': int(day) # XXX remove zero
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|