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
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 29,040 May-20-2025, 12:26 PM
Last Post: hanmen9527
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 606 May-01-2025, 03:43 PM
Last Post: Winfried
  Errors during run .exe file generated with Pyinstaller BushyAxis793 4 1,915 Mar-25-2025, 05:57 PM
Last Post: BushyAxis793
  Replace values in Yaml file with value in dictionary PelleH 1 2,410 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  How to remove unwanted images and tables from a Word file using Python? rownong 2 951 Feb-04-2025, 08:30 AM
Last Post: Pedroski55
  pass arguments from bat file to pyhon script from application absolut 2 1,444 Jan-13-2025, 11:05 AM
Last Post: DeaD_EyE
  Best way to feed python script of a file absolut 6 1,391 Jan-11-2025, 07:03 AM
Last Post: Gribouillis
  Load a Folium map into a pdf-file Thats_Leet 0 861 Jan-01-2025, 08:13 PM
Last Post: Thats_Leet
  Removal of watermark logo pdf file Python druva 0 902 Jan-01-2025, 11:55 AM
Last Post: druva
  How to write variable in a python file then import it in another python file? tatahuft 4 1,116 Jan-01-2025, 12:18 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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