Python Forum
xml file creation from an XML file template and data from an excel file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: xml file creation from an XML file template and data from an excel file (/thread-31587.html)



xml file creation from an XML file template and data from an excel file - naji_python - Dec-21-2020

I am a beginner and I am taking the python programming course, but at the same time I am trying to develop a program that allows
to create XML files to output:
1- an XML file model (for the structure)
2- an Excel file for the data
the contents of my excel file are as follows (as an example)

GROUP_ID NAME, Gender

1 NOM1 F
1 NOM2 1

2 NOM3 M
2 NOM4 2

for this example I want:

1- have 2 xml files: Id_GROUPE_1.xml (for data group 1) and Id_GROUPE_2.xml (for data group 2)

the content of the XML model file is as follows

<SOURCE DESCRIPTION ="" NAME ="GROUPEx" OBJECTVERSION ="1" OWNERNAME ="ADMIN" VERSIONNUMBER ="1">

<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="Id_GROUPE" VALUE ="x"/>

<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="NOM" VALUE ="NOMx"/>

<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="SEX" VALUE ="x"/>

/SOURCE>

the desired result for the first xml file (Id_GROUPE_1.xml):
<SOURCE DESCRIPTION ="" NAME ="GROUPE1" OBJECTVERSION ="1" OWNERNAME ="ADMIN" VERSIONNUMBER ="1">
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="Id_GROUPE" VALUE ="1"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="NOM" VALUE ="NOM1"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="SEX" VALUE ="F"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="Id_GROUPE" VALUE ="1"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="NOM" VALUE ="NOM2"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="SEX" VALUE ="M"/>
/SOURCE>

the desired result for the second xml file (Id_GROUPE_2.xml):
<SOURCE DESCRIPTION ="" NAME ="GROUPE2" OBJECTVERSION ="1" OWNERNAME ="ADMIN" VERSIONNUMBER ="1">
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="Id_GROUPE" VALUE ="2"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="NOM" VALUE ="NOM3"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="SEX" VALUE ="M"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="Id_GROUPE" VALUE ="2"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="NOM" VALUE ="NOM4"/>
<SOURCEFIELD BUSINESSNAME ="" DATATYPE ="varchar" NAME ="SEX" VALUE ="2"/>
/SOURCE>
For reading the Xlxs file I use the PANDAS library, the program which allows to read the xls file and the sotckage of information in a dictionary and the following:
# -*-coding:Latin-1 -*
import pandas as pd
import os   # On importe le module os qui dispose de variables 
            # et de fonctions utiles pour dialoguer avec votre 
            # système d'exploitation

# Récupération du dossier du script
root_dir = os.path.dirname(os.path.realpath(__file__))


# ------- Lecture du fichier Excel avec selection des colonnes
# En paramètre l'emplacement du fichier, l'onglet et les colonnes
df_xls = pd.read_excel(root_dir+"\\in\\exemple.xlsx",
sheet_name='TABLES', 
usecols=['Id_GROUPE','NOM','Sexe'])
               ,     
## -------Itère sur les lignes d'un Dataframe
groups = {}
for group in df_xls.itertuples():
    #d = {"Code_de_la_table": group.Code_de_la_table}
    d = {
     "Id_GROUPE": group.Id_GROUPE,
     "NOM": group.NOM,
     "Sexe": group.Sexe,
       }
    if group.Num_table not in groups:
        groups[group.Num_table] = [d]
    else:
        groups[group.Num_table].append(d)
#print(groups)

for key, value in groups.items():
 print (key,value)
for handling XML xfiles i want to use ElementTree module, but I don't know how to do it
if you have an idea or an example, thank you in advance


RE: xml file creation from an XML file template and data from an excel file - Gribouillis - Dec-21-2020

This PyMOTW-3 article explains how to use the ElementTree module and how to build documents with it (with example code).