Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • How to import data into excel Python Tutorial
  • Python Tutorial: Add a column to a data frame Python Dataframe
  • TypeError: List Indices Must Be Integers Or Slices, Not Tuple exceptions
  • Regular expressions python Python
  • How To Delete a Set of Keys From a Python Dictionary python dictionaries
  • TypeError: ‘float’ object is not callable Python
  • Python Tutorial: How to create charts in Excel Python Tutorial
  • how to compare two lists in Python Python Lists

How to Create an XML file from Excel using Python

Posted on October 4, 2021January 2, 2023 By admin 7 Comments on How to Create an XML file from Excel using Python

Estimated reading time: 3 minutes

Are you working on a data analytics project where you need to feed your data to a location that is able to process an XML file?

The ability to get your data into a structured format like XML has many benefits:

(A) You can transfer the data to a web service for processing.

(B) Multiple different formats of your raw data can be standardised, enabling quick conversion and processing.

(C) XML files can be read by multiple different programs, as long as you deliver them in the correct format.

(D) The receiver of data can easily read the XML file and store it on their database.

The ability to use this method to read and transfer data is a very powerful way to help a data analyst process large data sets.

In fact, if you are using cloud-based applications to analyse the information you are storing, this will quickly enable you to deliver the data.

What packages do I need in Python?

The first step is to import the following:

import pandas as pd
from lxml import etree as et

Next we want to read in the source data

In this instance, we are reading an excel file

raw_data = pd.read_excel(r'Link to where your data is stored including the full file name')

Now we want to start building the XML structure

The FIRST STEP is to define the root

root = et.Element('root')

The root is the parent of all the data items (tags) contained in the XML file and is needed as part of the structure

The SECOND STEP is to define the tag names that will store each row of the source data

for row in raw_data.iterrows(): ==> This is a loop that takes runs through each record and populates for each tag.
    root_tags = et.SubElement(root, 'ExportData') #=== > Root name
# These are the tag names for each row (SECTION 1)
    Column_heading_1 = et.SubElement(root_tags, 'Name')
    Column_heading_2 = et.SubElement(root_tags, 'Area')
    Column_heading_3 = et.SubElement(root_tags, 'NoPurchases')
    Column_heading_4 = et.SubElement(root_tags, 'Active')

###These are the values that will be populated for each row above
# The values inside the [] are the raw file column headings.(SECTION 2)
    Column_heading_1.text = str(row[1]['Name'])
    Column_heading_2.text = str(row[1]['Area'])
    Column_heading_3.text = str(row[1]['No Purchases'])
    Column_heading_4.text = str(row[1]['Active'])

The raw file looks like this:

NameAreaNo PurchasesActive
JohnDublin2Y
MaryGalway3N
JoeLimerick4N
JimmyKilkenny55Y
JenniferBelfast6N
SusanWaterford3Y
JakeCork1Y
BobbyDundalk11N
SarahSligo9N
CianEnnis8Y
Raw file data that will be imported into the XML file

The THIRD STEP is to create the XML file and populate it with the data from the source file

# This Section outputs the data to an xml file
# Unless you tell it otherwise it saves it to the same folder as the script.
tree = et.ElementTree(root) ==> The variable tree is to hold all the values of "root"
et.indent(tree, space="\t", level=0) ===> This just formats in a way that the XML is readable
tree.write('output.xml', encoding="utf-8") ==> The data is saved to an XML file

The XML output should look like the below

<root>
	<ExportData>
		<Name>John</Name>
		<Area>Dublin</Area>
		<NoPurchases>2</NoPurchases>
		<Active>Y</Active>
	</ExportData>
	<ExportData>
		<Name>Mary</Name>
		<Area>Galway</Area>
		<NoPurchases>3</NoPurchases>
		<Active>N</Active>
	</ExportData>
	<ExportData>
		<Name>Joe</Name>
		<Area>Limerick</Area>
		<NoPurchases>4</NoPurchases>
		<Active>N</Active>
	</ExportData>
	<ExportData>
		<Name>Jimmy</Name>
		<Area>Kilkenny</Area>
		<NoPurchases>55</NoPurchases>
		<Active>Y</Active>
	</ExportData>
	<ExportData>
		<Name>Jennifer</Name>
		<Area>Belfast</Area>
		<NoPurchases>6</NoPurchases>
		<Active>N</Active>
	</ExportData>
	<ExportData>
		<Name>Susan</Name>
		<Area>Waterford</Area>
		<NoPurchases>3</NoPurchases>
		<Active>Y</Active>
	</ExportData>
	<ExportData>
		<Name>Jake</Name>
		<Area>Cork</Area>
		<NoPurchases>1</NoPurchases>
		<Active>Y</Active>
	</ExportData>
	<ExportData>
		<Name>Bobby</Name>
		<Area>Dundalk</Area>
		<NoPurchases>11</NoPurchases>
		<Active>N</Active>
	</ExportData>
	<ExportData>
		<Name>Sarah</Name>
		<Area>Sligo</Area>
		<NoPurchases>9</NoPurchases>
		<Active>N</Active>
	</ExportData>
	<ExportData>
		<Name>Cian</Name>
		<Area>Ennis</Area>
		<NoPurchases>8</NoPurchases>
		<Active>Y</Active>
	</ExportData>
</root>

Additional XML data can be added

  1. Add more rows – All you need to do is add onto the source file and save. When you rerun the logic it will read in the extra information.
  2. Add more columns – All you need to do is go to the second step above add in a tag name to SECTION 1. Seperately you will need to add an additional column with data to the source file, and then add that column name to SECTION 2 as well

Python, Python Tutorial, Python working with excel, Python working with files, XML Tags:create an XML file, Data Analytics, Python, Python Tutorial, XML

Post navigation

Previous Post: How to Add Formulas to Excel using Python
Next Post: TypeError: List Indices Must Be Integers Or Slices, Not Tuple

Related Posts

  • Python Overview Interview Questions automation
  • How to Pass Python Variables to Javascript Javascript
  • TypeError: the first argument must be callable Python
  • What Is An Array In Python? array
  • How To Pass Data Between Functions Python Functions
  • How To Validate Cell Values In Excel Python

Comments (7) on “How to Create an XML file from Excel using Python”

  1. sharjeel Ahmad says:
    August 4, 2022 at 1:30 pm

    i am doing your way but it is giving me the error
    raise KeyError(key) from err
    KeyError: ‘Name’

    Reply
    1. admin says:
      August 20, 2022 at 2:45 pm

      Hi,

      Thanks for your message. Have you checked the source file the code reads from? The headings there should be the same as what is in the logic?

      That might be the cause of your issue.

      Thanks,

      Data analytics Ireland

      Reply
  2. Sara says:
    May 11, 2022 at 9:32 am

    How do you read each row in excel and get it as separate xml file. for example my row 1 of data is file1.xml, row 2 of data is file2.xml

    Reply
    1. admin says:
      May 22, 2022 at 12:58 pm

      Hi,

      Thanks for your message. I will look into your question for you and then revert with an answer.

      Data Analytics Ireland

      Reply
      1. admin says:
        May 22, 2022 at 3:21 pm

        Hi,

        Was doing some work on it here, have managed to create the separate files, just need to get them populated correctly now.

        I’ll provide an update as soon as I can.

        Data Analytics Ireland

        Reply
  3. Aayush says:
    November 15, 2021 at 8:51 am

    One of the best and easy way to convert xlsx or .csv file into xml. I have been looking for this for over a week and am so thankful sir for this easy to understand video. You have explained it using an example and this makes it all the more useful for beginners. Looking forward to learn more xml tutorial on your channel and this website.

    Thanks

    Reply
    1. admin says:
      November 17, 2021 at 10:08 pm

      Hi Thank you so much for your comment and feedback, it is really appreciated 🙂 I’ll look to do more soon around XML.

      Data analytics Ireland

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • ValueError: cannot convert float NaN to integer Null values
  • how to add sine and cosine in python code numpy
  • How To Join Tables In SQL SQL
  • how to create charts in Tkinter Python
  • How to Pass a Javascript Variable to Python using JSON Flask
  • TypeError: type object is not subscriptable strings
  • R Tutorial: How to pass data between functions R Programming
  • String Manipulation in Python python method

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT