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 to run a particular file in subprocess Shiri 5 265 Nov-24-2021, 09:29 AM
Last Post: ghoul
  Removing the unwanted data from a file jehoshua 12 554 Nov-15-2021, 08:54 PM
Last Post: jehoshua
  What to do when the requirements.txt file does not work Led_Zeppelin 2 284 Nov-04-2021, 06:51 PM
Last Post: snippsat
  Updating a config file [solved] ebolisa 8 414 Nov-04-2021, 10:20 AM
Last Post: Gribouillis
  Hashing an address for binary file Python_help 8 406 Nov-04-2021, 06:23 AM
Last Post: ndc85430
Photo (Beginners problem) Does file change during this code? fiqu 3 273 Nov-03-2021, 10:23 PM
Last Post: bowlofred
  Extract a string between 2 words from a text file OscarBoots 2 260 Nov-02-2021, 08:50 AM
Last Post: ibreeden
Photo how I write the output into xml file in python? 3lnyn0 1 243 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  Create a 2-channel numpy file from two cvs file containing a 9x9 matrix silvialecc 1 297 Oct-26-2021, 07:59 AM
Last Post: Gribouillis
  Rename Files based on XML file klturi421 3 310 Oct-22-2021, 07:37 PM
Last Post: klturi421

Forum Jump:

User Panel Messages

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