Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.94 KB | None | 0 0
  1. def XML_import_BOLZANO ():
  2.     '''Routine di importazione di un prezziario XML formato SIX. Molto
  3.    liberamente tratta da PreventARES https://launchpad.net/preventares
  4.    di <Davide Vescovini> <davide.vescovini@gmail.com>
  5.    Versione bilingue'''
  6.     filename = filedia('Scegli il file XML-SIX da convertire...')
  7.     #~ filename = '/media/giuserpe/PRIVATO/_dwg/ULTIMUSFREE/elenchi/Bolzano/2015/prova_ZIX.xml'
  8.     # inizializzazioe delle variabili
  9.     lista_articoli = list() # lista in cui memorizzare gli articoli da importare
  10.     diz_um = dict() # array per le unità di misura
  11.     # stringhe per descrizioni articoli
  12.     desc_breve = str()
  13.     desc_estesa = str()
  14.     # effettua il parsing del file XML
  15.     tree = ElementTree()
  16.     tree.parse(filename)
  17.     # ottieni l'item root
  18.     root = tree.getroot()
  19.     logging.debug(list(root))
  20.     # effettua il parsing di tutti gli elemnti dell'albero XML
  21.     iter = tree.getiterator()
  22.     listaSOA = []
  23.     articolo = []
  24.     lingua_scelta = 'it'
  25. ########################################################################
  26.     # nome del prezzario
  27.     prezzario = root.find('{six.xsd}prezzario')
  28.     if len(prezzario.findall('{six.xsd}przDescrizione')) == 2:
  29.         if prezzario.findall('{six.xsd}przDescrizione')[0].get('lingua') == lingua_scelta:
  30.             nome1 = prezzario.findall('{six.xsd}przDescrizione')[0].get('breve')
  31.             nome2 = prezzario.findall('{six.xsd}przDescrizione')[1].get('breve')
  32.         else:
  33.             nome1 = prezzario.findall('{six.xsd}przDescrizione')[1].get('breve')
  34.             nome2 = prezzario.findall('{six.xsd}przDescrizione')[0].get('breve')
  35.         nome=nome1+'\n§\n'+nome2
  36.     else:
  37.         nome = prezzario.findall('{six.xsd}przDescrizione')[0].get('breve')
  38. ########################################################################
  39.     suffB_IT, suffE_IT, suffB_DE, suffE_DE = '', '', '', ''
  40.     test = True
  41.     for elem in iter:
  42.         # esegui le verifiche sulla root dell'XML
  43.         if elem.tag == '{six.xsd}intestazione':
  44.             intestazioneId= elem.get('intestazioneId')
  45.             lingua= elem.get('lingua')
  46.             separatore= elem.get('separatore')
  47.             separatoreParametri= elem.get('separatoreParametri')
  48.             valuta= elem.get('valuta')
  49.             autore= elem.get('autore')
  50.             versione= elem.get('versione')
  51.         elif elem.tag == '{six.xsd}categoriaSOA':
  52.             soaId = elem.get('soaId')
  53.             soaCategoria = elem.get ('soaCategoria')
  54.             soaDescrizione = elem.find('{six.xsd}soaDescrizione')
  55.             if soaDescrizione != None:
  56.                 breveSOA = soaDescrizione.get('breve')
  57.             voceSOA = (soaCategoria, soaId, breveSOA)
  58.             listaSOA.append(voceSOA)
  59.         elif elem.tag == '{six.xsd}prezzario':
  60.             prezzarioId = elem.get('prezzarioId')
  61.             przId= elem.get('przId')
  62.             livelli_struttura= len(elem.get('prdStruttura').split('.'))
  63.             categoriaPrezzario= elem.get('categoriaPrezzario')
  64. ########################################################################
  65.         elif elem.tag == '{six.xsd}unitaDiMisura':
  66.             um_id= elem.get('unitaDiMisuraId')
  67.             um_sim= elem.get('simbolo')
  68.             um_dec= elem.get('decimali')
  69.             # crea il dizionario dell'unita di misura
  70. ########################################################################
  71.             #~ unità di misura
  72.             unita_misura = ''
  73.             try:
  74.                 if len (elem.findall('{six.xsd}udmDescrizione')) == 1:
  75.                     unita_misura = elem.findall('{six.xsd}udmDescrizione')[0].get('breve')
  76.                 else:
  77.                     if elem.findall('{six.xsd}udmDescrizione')[1].get('lingua') == lingua_scelta:
  78.                         unita_misura1 = elem.findall('{six.xsd}udmDescrizione')[1].get('breve')
  79.                         unita_misura2 = elem.findall('{six.xsd}udmDescrizione')[0].get('breve')
  80.                     else:
  81.                         unita_misura1 = elem.findall('{six.xsd}udmDescrizione')[0].get('breve')
  82.                         unita_misura2 = elem.findall('{six.xsd}udmDescrizione')[1].get('breve')
  83.                 if unita_misura != None:
  84.                     unita_misura = unita_misura1 +' § '+ unita_misura2
  85.             except IndexError:
  86.                 pass
  87.             diz_um[um_id] = unita_misura
  88. ########################################################################
  89.         # se il tag è un prodotto fa parte degli articoli da analizzare
  90.         elif elem.tag == '{six.xsd}prodotto':
  91.            
  92.             prod_id = elem.get('prodottoId')
  93.             if prod_id is not None:
  94.                 prod_id = int(prod_id)
  95.             tariffa= elem.get('prdId')
  96.             sic = elem.get('onereSicurezza')
  97.             if sic != None:
  98.                 sicurezza = float(sic)
  99.             else:
  100.                 sicurezza = ''
  101. ########################################################################
  102.             if diz_um.get(elem.get('unitaDiMisuraId')) != None:
  103.                 unita_misura = diz_um.get(elem.get('unitaDiMisuraId'))
  104.             else:
  105.                 unita_misura = ''
  106. ########################################################################
  107.             # verifica e ricava le sottosezioni
  108.             sub_mdo = elem.find('{six.xsd}incidenzaManodopera')
  109.             if sub_mdo != None:
  110.                 mdo = float(sub_mdo.text)
  111.             else:
  112.                 mdo =''
  113. ########################################################################
  114.             # descrizione voci
  115.             desc_estesa1, desc_estesa2 = '', ''
  116.             if test == 0:
  117.                 test = 1
  118.                 suffB_IT = suffB_IT + ' '
  119.                 suffE_IT = suffE_IT + ' '
  120.                 suffB_DE = suffB_DE + ' '
  121.                 suffE_DE = suffE_DE + ' '
  122.             try:
  123.                 if len (elem.findall('{six.xsd}prdDescrizione')) == 1:
  124.                     desc_breve = elem.findall('{six.xsd}prdDescrizione')[0].get('breve')
  125.                     desc_estesa = elem.findall('{six.xsd}prdDescrizione')[0].get('estesa')
  126.                 else:
  127.             #descrizione voce
  128.                     if elem.findall('{six.xsd}prdDescrizione')[0].get('lingua') == lingua_scelta:
  129.                         desc_breve1  = elem.findall('{six.xsd}prdDescrizione')[0].get('breve')
  130.                         desc_breve2  = elem.findall('{six.xsd}prdDescrizione')[1].get('breve')
  131.                         desc_estesa1 = elem.findall('{six.xsd}prdDescrizione')[0].get('estesa')
  132.                         desc_estesa2 = elem.findall('{six.xsd}prdDescrizione')[1].get('estesa')
  133.                     else:
  134.                         desc_breve1  = elem.findall('{six.xsd}prdDescrizione')[1].get('breve')
  135.                         desc_breve2  = elem.findall('{six.xsd}prdDescrizione')[0].get('breve')
  136.                         desc_estesa1 = elem.findall('{six.xsd}prdDescrizione')[1].get('estesa')
  137.                         desc_estesa2 = elem.findall('{six.xsd}prdDescrizione')[0].get('estesa')
  138.                     if desc_breve1 == None:
  139.                         desc_breve1 = ''
  140.                     if desc_breve2 == None:
  141.                         desc_breve2 = ''
  142.                     if desc_estesa1 == None:
  143.                         desc_estesa1 = ''
  144.                     if desc_estesa2 == None:
  145.                         desc_estesa2 = ''
  146.                     desc_breve = suffB_IT + desc_breve1.strip() +'\n§\n'+ suffB_DE + desc_breve2.strip()
  147.                     desc_estesa = suffE_IT + desc_estesa1.strip() +'\n§\n'+ suffE_DE + desc_estesa2.strip()
  148.                 if len(desc_breve) > len (desc_estesa):
  149.                     desc_voce = desc_breve
  150.                 else:
  151.                     desc_voce = desc_estesa
  152.             except IndexError:
  153.                 pass
  154. ########################################################################
  155.             sub_quot = elem.find('{six.xsd}prdQuotazione')
  156.             if sub_quot != None:
  157.                 list_nr = sub_quot.get('listaQuotazioneId')
  158.                 if sub_quot.get('valore') != None:
  159.                     valore = float(sub_quot.get('valore'))
  160.                 if valore == 0:
  161.                     valore = ''
  162.                 if sub_quot.get('quantita') is not None: #SERVE DAVVERO???
  163.                     quantita = float(sub_quot.get('quantita'))
  164.             else:
  165.                 test = 0
  166.                 suffB_IT, suffB_DE, suffE_IT, suffE_DE = desc_breve1, desc_breve2, desc_estesa1, desc_estesa2
  167.                 valore = ''
  168.                 quantita = ''
  169.             articolo = (prod_id,            #0
  170.                         tariffa,            #1
  171.                         desc_voce,        #2
  172.                         desc_estesa,        #3 non usata
  173.                         unita_misura,       #4
  174.                         valore,             #5
  175.                         quantita,           #6
  176.                         mdo,                #7
  177.                         sicurezza)         #8 %
  178.             lista_articoli.append(articolo)
  179. # compilo la tabella ###################################################
  180.     oDoc = XSCRIPTCONTEXT.getDocument()
  181.     oSheet = oDoc.getSheets().getByName('Listino')
  182.     oSheet.getCellRangeByName('G1').Value = 1 #Livello di accodamento
  183.     if oSheet.getCellRangeByName('F5').String == '': #se la cella F5 è vuota, elimina riga
  184.         oRangeAddress = oSheet.getCellRangeByName('A5:B5').getRangeAddress()
  185.         oSheet.removeRange(oRangeAddress, 3) # Mode.ROWS
  186. # nome del prezzario ###################################################
  187.     oSheet.getCellRangeByName('C1').String = nome
  188.     scarto=6 #primo rigo dati
  189.     for riga, elem in enumerate(lista_articoli):
  190. # ciclo for ottimizzato da Dabniele Zambelli https://plus.google.com/u/0/107607056036247518268/about
  191.         riga += scarto
  192.         oSheet.getCellByPosition(0, riga).String = elem[0]    #prod_id
  193.         oSheet.getCellByPosition(2, riga).String = elem[1]    #tariffa
  194.         oSheet.getCellByPosition(4, riga).String = elem[2]    #desc_voce
  195.         oSheet.getCellByPosition(6, riga).String = elem[4]    #unita_misura
  196.         if elem[5] != '':
  197.             oSheet.getCellByPosition(7, riga).Value = elem[5] #valore
  198.         if elem[7] != '' and elem[7] != 0:
  199.             oSheet.getCellByPosition(8, riga).Value = elem[7] /100 #manodopera % (lo stile % di LibreOffice moltiplica per 100)
  200.         if elem[8] != '':
  201.             oSheet.getCellByPosition(11, riga).Value = float (elem[5] * elem[8] / 100) #sicurezza
  202.     eRow = uFindString ('ATTENZIONE!', oSheet)[1]+1
  203.     lastRow = getLastUsedCell(oSheet).EndRow+1
  204.     oSheet.getCellRangeByPosition (0,eRow,5,lastRow).CellStyle = 'List-stringa-sin'
  205.     oSheet.getCellRangeByPosition (6,eRow,6,lastRow).CellStyle = 'List-stringa-centro'
  206.     oSheet.getCellRangeByPosition (7,eRow,11,lastRow).CellStyle = 'List-num-euro'
  207.     oSheet.getCellRangeByPosition (8,eRow,8,lastRow).CellStyle = 'List-%'
  208.     oSheet.getCellRangeByPosition (10,eRow,10,lastRow).CellStyle = 'List-%'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement