Python Forum
xml file creation from an XML file template and data from an excel file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml file creation from an XML file template and data from an excel file
#1
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
Reply
#2
This PyMOTW-3 article explains how to use the ElementTree module and how to build documents with it (with example code).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File upload with the Flask framework (Errno 2) Username_Python1 0 100 Mar-26-2024, 11:49 AM
Last Post: Username_Python1
  Compare current date on calendar with date format file name Fioravanti 1 103 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Decoding lat/long in file name johnmcd 4 265 Mar-22-2024, 11:51 AM
Last Post: johnmcd
  Can rich.logging output to file? pyfoo 1 184 Mar-21-2024, 10:30 AM
Last Post: pyfoo
  FTP Download of Last File jland47 4 287 Mar-16-2024, 09:15 AM
Last Post: Pedroski55
  File is not being created with 'w' argument CAD79 3 318 Mar-14-2024, 12:05 PM
Last Post: snippsat
  help with accessing .txt file and performing actions SamWestlakeCann 4 301 Mar-07-2024, 07:33 PM
Last Post: deanhystad
  Matching string from a file tester_V 5 342 Mar-05-2024, 05:46 AM
Last Post: Danishhafeez
  Python openyxl not updating Excel file MrBean12 1 249 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Preprocessing an OBJ file, what is the best way to? KeithSloan 7 435 Mar-02-2024, 04:48 PM
Last Post: KeithSloan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020