Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def XML_import_BOLZANO ():
- '''Routine di importazione di un prezziario XML formato SIX. Molto
- liberamente tratta da PreventARES https://launchpad.net/preventares
- di <Davide Vescovini> <davide.vescovini@gmail.com>
- Versione bilingue'''
- filename = filedia('Scegli il file XML-SIX da convertire...')
- #~ filename = '/media/giuserpe/PRIVATO/_dwg/ULTIMUSFREE/elenchi/Bolzano/2015/prova_ZIX.xml'
- # inizializzazioe delle variabili
- lista_articoli = list() # lista in cui memorizzare gli articoli da importare
- diz_um = dict() # array per le unità di misura
- # stringhe per descrizioni articoli
- desc_breve = str()
- desc_estesa = str()
- # effettua il parsing del file XML
- tree = ElementTree()
- tree.parse(filename)
- # ottieni l'item root
- root = tree.getroot()
- logging.debug(list(root))
- # effettua il parsing di tutti gli elemnti dell'albero XML
- iter = tree.getiterator()
- listaSOA = []
- articolo = []
- lingua_scelta = 'it'
- ########################################################################
- # nome del prezzario
- prezzario = root.find('{six.xsd}prezzario')
- if len(prezzario.findall('{six.xsd}przDescrizione')) == 2:
- if prezzario.findall('{six.xsd}przDescrizione')[0].get('lingua') == lingua_scelta:
- nome1 = prezzario.findall('{six.xsd}przDescrizione')[0].get('breve')
- nome2 = prezzario.findall('{six.xsd}przDescrizione')[1].get('breve')
- else:
- nome1 = prezzario.findall('{six.xsd}przDescrizione')[1].get('breve')
- nome2 = prezzario.findall('{six.xsd}przDescrizione')[0].get('breve')
- nome=nome1+'\n§\n'+nome2
- else:
- nome = prezzario.findall('{six.xsd}przDescrizione')[0].get('breve')
- ########################################################################
- suffB_IT, suffE_IT, suffB_DE, suffE_DE = '', '', '', ''
- test = True
- for elem in iter:
- # esegui le verifiche sulla root dell'XML
- if elem.tag == '{six.xsd}intestazione':
- intestazioneId= elem.get('intestazioneId')
- lingua= elem.get('lingua')
- separatore= elem.get('separatore')
- separatoreParametri= elem.get('separatoreParametri')
- valuta= elem.get('valuta')
- autore= elem.get('autore')
- versione= elem.get('versione')
- elif elem.tag == '{six.xsd}categoriaSOA':
- soaId = elem.get('soaId')
- soaCategoria = elem.get ('soaCategoria')
- soaDescrizione = elem.find('{six.xsd}soaDescrizione')
- if soaDescrizione != None:
- breveSOA = soaDescrizione.get('breve')
- voceSOA = (soaCategoria, soaId, breveSOA)
- listaSOA.append(voceSOA)
- elif elem.tag == '{six.xsd}prezzario':
- prezzarioId = elem.get('prezzarioId')
- przId= elem.get('przId')
- livelli_struttura= len(elem.get('prdStruttura').split('.'))
- categoriaPrezzario= elem.get('categoriaPrezzario')
- ########################################################################
- elif elem.tag == '{six.xsd}unitaDiMisura':
- um_id= elem.get('unitaDiMisuraId')
- um_sim= elem.get('simbolo')
- um_dec= elem.get('decimali')
- # crea il dizionario dell'unita di misura
- ########################################################################
- #~ unità di misura
- unita_misura = ''
- try:
- if len (elem.findall('{six.xsd}udmDescrizione')) == 1:
- unita_misura = elem.findall('{six.xsd}udmDescrizione')[0].get('breve')
- else:
- if elem.findall('{six.xsd}udmDescrizione')[1].get('lingua') == lingua_scelta:
- unita_misura1 = elem.findall('{six.xsd}udmDescrizione')[1].get('breve')
- unita_misura2 = elem.findall('{six.xsd}udmDescrizione')[0].get('breve')
- else:
- unita_misura1 = elem.findall('{six.xsd}udmDescrizione')[0].get('breve')
- unita_misura2 = elem.findall('{six.xsd}udmDescrizione')[1].get('breve')
- if unita_misura != None:
- unita_misura = unita_misura1 +' § '+ unita_misura2
- except IndexError:
- pass
- diz_um[um_id] = unita_misura
- ########################################################################
- # se il tag è un prodotto fa parte degli articoli da analizzare
- elif elem.tag == '{six.xsd}prodotto':
- prod_id = elem.get('prodottoId')
- if prod_id is not None:
- prod_id = int(prod_id)
- tariffa= elem.get('prdId')
- sic = elem.get('onereSicurezza')
- if sic != None:
- sicurezza = float(sic)
- else:
- sicurezza = ''
- ########################################################################
- if diz_um.get(elem.get('unitaDiMisuraId')) != None:
- unita_misura = diz_um.get(elem.get('unitaDiMisuraId'))
- else:
- unita_misura = ''
- ########################################################################
- # verifica e ricava le sottosezioni
- sub_mdo = elem.find('{six.xsd}incidenzaManodopera')
- if sub_mdo != None:
- mdo = float(sub_mdo.text)
- else:
- mdo =''
- ########################################################################
- # descrizione voci
- desc_estesa1, desc_estesa2 = '', ''
- if test == 0:
- test = 1
- suffB_IT = suffB_IT + ' '
- suffE_IT = suffE_IT + ' '
- suffB_DE = suffB_DE + ' '
- suffE_DE = suffE_DE + ' '
- try:
- if len (elem.findall('{six.xsd}prdDescrizione')) == 1:
- desc_breve = elem.findall('{six.xsd}prdDescrizione')[0].get('breve')
- desc_estesa = elem.findall('{six.xsd}prdDescrizione')[0].get('estesa')
- else:
- #descrizione voce
- if elem.findall('{six.xsd}prdDescrizione')[0].get('lingua') == lingua_scelta:
- desc_breve1 = elem.findall('{six.xsd}prdDescrizione')[0].get('breve')
- desc_breve2 = elem.findall('{six.xsd}prdDescrizione')[1].get('breve')
- desc_estesa1 = elem.findall('{six.xsd}prdDescrizione')[0].get('estesa')
- desc_estesa2 = elem.findall('{six.xsd}prdDescrizione')[1].get('estesa')
- else:
- desc_breve1 = elem.findall('{six.xsd}prdDescrizione')[1].get('breve')
- desc_breve2 = elem.findall('{six.xsd}prdDescrizione')[0].get('breve')
- desc_estesa1 = elem.findall('{six.xsd}prdDescrizione')[1].get('estesa')
- desc_estesa2 = elem.findall('{six.xsd}prdDescrizione')[0].get('estesa')
- if desc_breve1 == None:
- desc_breve1 = ''
- if desc_breve2 == None:
- desc_breve2 = ''
- if desc_estesa1 == None:
- desc_estesa1 = ''
- if desc_estesa2 == None:
- desc_estesa2 = ''
- desc_breve = suffB_IT + desc_breve1.strip() +'\n§\n'+ suffB_DE + desc_breve2.strip()
- desc_estesa = suffE_IT + desc_estesa1.strip() +'\n§\n'+ suffE_DE + desc_estesa2.strip()
- if len(desc_breve) > len (desc_estesa):
- desc_voce = desc_breve
- else:
- desc_voce = desc_estesa
- except IndexError:
- pass
- ########################################################################
- sub_quot = elem.find('{six.xsd}prdQuotazione')
- if sub_quot != None:
- list_nr = sub_quot.get('listaQuotazioneId')
- if sub_quot.get('valore') != None:
- valore = float(sub_quot.get('valore'))
- if valore == 0:
- valore = ''
- if sub_quot.get('quantita') is not None: #SERVE DAVVERO???
- quantita = float(sub_quot.get('quantita'))
- else:
- test = 0
- suffB_IT, suffB_DE, suffE_IT, suffE_DE = desc_breve1, desc_breve2, desc_estesa1, desc_estesa2
- valore = ''
- quantita = ''
- articolo = (prod_id, #0
- tariffa, #1
- desc_voce, #2
- desc_estesa, #3 non usata
- unita_misura, #4
- valore, #5
- quantita, #6
- mdo, #7
- sicurezza) #8 %
- lista_articoli.append(articolo)
- # compilo la tabella ###################################################
- oDoc = XSCRIPTCONTEXT.getDocument()
- oSheet = oDoc.getSheets().getByName('Listino')
- oSheet.getCellRangeByName('G1').Value = 1 #Livello di accodamento
- if oSheet.getCellRangeByName('F5').String == '': #se la cella F5 è vuota, elimina riga
- oRangeAddress = oSheet.getCellRangeByName('A5:B5').getRangeAddress()
- oSheet.removeRange(oRangeAddress, 3) # Mode.ROWS
- # nome del prezzario ###################################################
- oSheet.getCellRangeByName('C1').String = nome
- scarto=6 #primo rigo dati
- for riga, elem in enumerate(lista_articoli):
- # ciclo for ottimizzato da Dabniele Zambelli https://plus.google.com/u/0/107607056036247518268/about
- riga += scarto
- oSheet.getCellByPosition(0, riga).String = elem[0] #prod_id
- oSheet.getCellByPosition(2, riga).String = elem[1] #tariffa
- oSheet.getCellByPosition(4, riga).String = elem[2] #desc_voce
- oSheet.getCellByPosition(6, riga).String = elem[4] #unita_misura
- if elem[5] != '':
- oSheet.getCellByPosition(7, riga).Value = elem[5] #valore
- if elem[7] != '' and elem[7] != 0:
- oSheet.getCellByPosition(8, riga).Value = elem[7] /100 #manodopera % (lo stile % di LibreOffice moltiplica per 100)
- if elem[8] != '':
- oSheet.getCellByPosition(11, riga).Value = float (elem[5] * elem[8] / 100) #sicurezza
- eRow = uFindString ('ATTENZIONE!', oSheet)[1]+1
- lastRow = getLastUsedCell(oSheet).EndRow+1
- oSheet.getCellRangeByPosition (0,eRow,5,lastRow).CellStyle = 'List-stringa-sin'
- oSheet.getCellRangeByPosition (6,eRow,6,lastRow).CellStyle = 'List-stringa-centro'
- oSheet.getCellRangeByPosition (7,eRow,11,lastRow).CellStyle = 'List-num-euro'
- oSheet.getCellRangeByPosition (8,eRow,8,lastRow).CellStyle = 'List-%'
- oSheet.getCellRangeByPosition (10,eRow,10,lastRow).CellStyle = 'List-%'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement