import time import xlrd import xmltodict from netforce.model import Model, fields, get_model from netforce.access import get_active_company from netforce.utils import get_file_path from netforce.utils import get_data_path from netforce.database import get_connection class ImportPayment(Model): _name="clinic.import.payment" _transient=True _fields={ 'date': fields.DateTime("Date"), 'file': fields.File("File"), } _defaults={ 'date': lambda *a: time.strftime("%Y-%m-%d %H:%M:%S"), } def read_excel(self,fpath=None): 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) worksheet=wb.sheet_by_name("Sheet1") num_rows=worksheet.nrows-1 curr_row=-1 while curr_row < num_rows: curr_row +=1 worksheet.cell_value(curr_row,2) return data def read_xml(self,fpath=None,node=""): data={} if not node: 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()) stmstm=data.get('STMSTM') if stmstm: hdbills=stmstm.get(node) if not hdbills: return {} lines=[] for k, v in hdbills.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) #titles=[title for title, value in lines[0].items()] return lines def import_nhso(self,ids,context={}): obj=self.browse(ids)[0] fname=obj.file fpath=get_file_path(fname) if not fpath: raise Exception("Please select file") lines=self.read_xml(fpath,node='HDBills') if not lines: raise Exception("Wrong file") for line in lines: print(line) def import_mg(self,ids,context={}): obj=self.browse(ids)[0] fname=obj.file fpath=get_file_path(fname) print("fpath ", fpath) def import_sc(self,ids,context={}): obj=self.browse(ids)[0] fname=obj.file fpath=get_file_path(fname) print("fpath ", fpath) ImportPayment.register()