1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| # coding: utf-8
import xlrd
import xml.dom.minidom
import string
import sys
import os
import datetime
import tkinter
from tkinter.filedialog import askopenfilename
'''
Created on 21 aout 2017
@author: moi
'''
def explorateur_window(repertoireinit):
""" Explorateur Windows """
root = tkinter.Tk()
filename = tkinter.filedialog.askopenfilename(
initialdir=repertoireinit,
title="Choisir votre fichier",
filetypes=(
("Fichier xlsx", "*.xlsx"),
("Tous type de fichier","*.*")
)
)
return filename
pass
def SoapIn(docxml):
""" bloc soapui ouverture"""
# lignedb = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
# lignedb = '\n' + tab_4 + lignedb + '<soapenv:Body>'
# lignefn = '\n' +' </soapenv:Body>'
# lignefn = tab_4 + lignefn + '\n' + '</soapenv:Envelope>
Soapenv_Envelope_ouvrant = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
Soapenv_body_ouvrant = '<soapenv:Body>'
Soapenv_Envelope_fermante = ' </soapenv:Body>'
Soapenv_body__fermante = '</soapenv:Envelope>'
rt_docxml = Soapenv_Envelope_ouvrant + Soapenv_body_ouvrant + docxml + Soapenv_Envelope_fermante + Soapenv_body__fermante
return rt_docxml
def SoapOut(docxml):
""" bloc soapui fermeture"""
Soap_Envelope_ouvrant = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
Soap_body_ouvrant = '<soap:Body>'
Soap_Envelope_fermante = ' </soap:Body>'
Soap_body__fermante = '</soap:Envelope>'
rt_docxml = Soap_Envelope_ouvrant + Soap_body_ouvrant + docxml + Soap_Envelope_fermante + Soap_body__fermante
return rt_docxml
def LireExcel(fichier):
# renvoie les noms des feuilles du fichier sous forme de liste.
liste_feuille = fichier.sheet_names()
feuille = fichier.sheet_by_index(0) # feuile N�1
la_feuille = feuille.col(1) # contenu de la colonne N�2 type cell
nb_ligne = feuille.nrows # le nombre de lignes
#print(nb_ligne)
# traitement de la colonne N�1
extracdata(la_feuille , 1,feuille)
print("traitement type 1, terminer")
la_feuille_r = feuille.col(2)
print("extraction de la seconde colonne")
extracdata(la_feuille_r , 2, feuille)
print("traitement type 2, terminer")
pass
def extracdata(liste_feuille , type, feuille):
numcas = 0
for lesdata in liste_feuille:
numcas = numcas + 1
#myCell.value : rowInd, colInd renvoie la valeur.
numdos = feuille.cell_value(numcas, 5)
print(type)
# Ouverture du fichier destination
if lesdata.value != 'VIDE': # construction des noms de fichier
if type == 1:
nomfic ='NUMCAS_'+ str(numcas) + '_in.xml'
ligneecrire= SoapIn(lesdata.value)
else:
nomfic ='NUMCAS_'+ str(numcas) + '_out.xml'
ligneecrire= SoapOut(lesdata.value)
# Reconstute le nom du fichier
nom_fichier_path_out = os.path.join(nom_fichier_path, nomfic)
dom = xml.dom.minidom.parseString(ligneecrire) # convertie le text en arbre xml DOM
dom_xml = dom.toprettyxml(indent=" ", newl="\n" ) # formattage XML soap
# Reconstute le nom du fichier
nom_fichier_path_out = os.path.join(nom_fichier_path, nomfic)
destination = open(nom_fichier_path_out , "w") # ouverture du fichier de sortie
#dom_xml = dom.toprettyxml()
destination.write(dom_xml)
# Fermeture du fichier destination
destination.close()
# convertieunix(nomfic)
pass
if __name__ == '__main__':
# ouverture du fichier Excel
nom_fichier_path_in = explorateur_window ("C://") # Recherche via explorateur
if os.path.isfile(nom_fichier_path_in):
# ouverture du fichier
wb = xlrd.open_workbook(nom_fichier_path_in)
# Ouverture du fichier destination
nom_fichier_in = os.path.basename(nom_fichier_path_in) # Retourne le dernier �l�ment d'un chemin
nom_fichier_path = os.path.dirname(nom_fichier_path_in) # Retourne le dossier parent de l'�l�ment
list_nf = os.path.split(nom_fichier_path_in) # Fractionne un chemin d'acc�s. Retourne un tuple
try:
if os.path.isfile(nom_fichier_path_in):
LireExcel(wb)
finally:
print('Termine') |
Partager