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
  Automated filler for an Excel form is not writing the data Quian34 2 36 Jun-10-2026, 07:27 AM
Last Post: Larz60+
  Control of the number of daemon error messages to the file MarPan 3 87 Mar-25-2026, 11:00 AM
Last Post: DeaD_EyE
  open a text file using list() Pedroski55 2 115 Feb-25-2026, 06:57 PM
Last Post: noisefloor
  [fixed] distinguish file name from the path paul18fr 5 126 Feb-13-2026, 02:49 PM
Last Post: DeaD_EyE
  why: pyinstaller generates a different exe file every time momo1313 1 74 Jan-23-2026, 03:17 PM
Last Post: DeaD_EyE
  If I open a file write or append, is the file loaded into RAM? Pedroski55 11 1,118 Jan-14-2026, 07:49 AM
Last Post: Pedroski55
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 7,273 Dec-16-2025, 05:29 PM
Last Post: zodazy
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 858 Nov-04-2025, 11:51 AM
Last Post: buran
  Errors using json file RonR 5 955 Oct-28-2025, 03:00 PM
Last Post: buran
  .xlsm file can't be opened after writing data to one worksheet mhyatt 4 885 Oct-11-2025, 11:25 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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