create a csv file which is Excel friendly format.

import xml.etree.ElementTree as ET
from os import listdir


xml_lst = [f for f in listdir() if f.startswith('xml')]
fields = ['RecordID','I_25Hz_1s','I_75Hz_2s'] # TODO - add rest of the fields
with open('out.csv','w') as f:
  f.write(','.join(fields) + '\n')
  for xml in xml_lst:
    root = ET.parse(xml)
    values = [root.find(f'.//{f}').text for f in fields]
    f.write(','.join(values) + '\n')

output

RecordID,I_25Hz_1s,I_75Hz_2s
Madird01,56.40,0.36
London01,56.40,0.36
Answer from balderman on Stack Overflow
🌐
PyPI
pypi.org › project › xml2xlsx
xml2xlsx · PyPI
This is a simple example to demonstrate the feature: ... <sheet> <row> <cell ref-append="mylist">1</cell> <cell ref-append="mylist">2</cell> </row> <row><cell ref-append="mylist">3</cell></row> <row><cell>=SUM({mylist})</cell></row> </sheet> This ...
      » pip install xml2xlsx
    
Published   Sep 16, 2024
Version   1.0.2
🌐
Python.org
discuss.python.org › python help
Convert xml to excel/csv - Python Help - Discussions on Python.org
October 15, 2022 - Please help me in converting XML file into excel/csv. Thank you in advance.
Discussions

How to parse XML into an excel sheet?
Depending on the complexity of the xml, the easiest way might be to simply read into a pandas dataframe and write it back out as Excel. https://pandas.pydata.org/docs/reference/api/pandas.read_xml.html# https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html More on reddit.com
🌐 r/learnpython
2
3
December 2, 2021
Python extract data from xml and save it to excel - Stack Overflow
I would like to extract some data from an XML file and save it in a table format, such as XLS or DBF. Here is XML file i have: More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
How to import an XML file into an Excel XLS file template using Python? - Stack Overflow
I have an Excel template file which has columns as defined within an XML. Manually I can right click over the template then XML > import, and select the XML file, and finally save the file. How ca... More on stackoverflow.com
🌐 stackoverflow.com
Convert Excel XML to .xlsx with python
Hello guys, so i have this XML file which i can open pretty easily using Excel this is the file, but i want to convert it to xlsx or any compatible format to be able to use openpyxl module to it so that i can read it easily, is there any way to do this on python? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
September 29, 2020
Top answer
1 of 3
2

create a csv file which is Excel friendly format.

import xml.etree.ElementTree as ET
from os import listdir


xml_lst = [f for f in listdir() if f.startswith('xml')]
fields = ['RecordID','I_25Hz_1s','I_75Hz_2s'] # TODO - add rest of the fields
with open('out.csv','w') as f:
  f.write(','.join(fields) + '\n')
  for xml in xml_lst:
    root = ET.parse(xml)
    values = [root.find(f'.//{f}').text for f in fields]
    f.write(','.join(values) + '\n')

output

RecordID,I_25Hz_1s,I_75Hz_2s
Madird01,56.40,0.36
London01,56.40,0.36
2 of 3
1

When you need to iterate over files in folder with similar names one of the ways could be make a pattern and use glob. To make sure that returned path is file you can use isfile().

Regarding XML, I see that basically you need to write values of every terminal tag in column with name of this tag. As you have various files you can create tag-value dictionaries from each file and store them into ChainMap. After all files processed you can use DictWriter to write all data into final csv file.

This method is much more safe and flexible then use static column names. Firstly program will collect all possible tag(column) names from all files, so in case if XML doesn't have such a tag or have some extra tags it won't throw an exception and all data will be saved.

Code:

import xml.etree.ElementTree as ET
from glob import iglob
from os.path import isfile, join
from csv import DictWriter
from collections import ChainMap

xml_root = r"C:\data\Desktop\Blue\XML-files"
pattern = "xmlfile_*"
data = ChainMap()
for filename in iglob(join(xml_root, pattern)):
    if isfile(filename):
        tree = ET.parse(filename)
        root = tree.getroot()
        temp = {node.tag: node.text for node in root.iter() if not node}
        data = data.new_child(temp)

with open(join(xml_root, "data.csv"), "w", newline="") as f:
    writer = DictWriter(f, data)
    writer.writeheader()
    writer.writerows(data.maps[:-1])  # last is empty dict

Upd. If you want to use xlsx format instead of csv you have to use third-party library (e.g. openpyxl). Example of usage:

from openpyxl import Workbook

...

wb = Workbook(write_only=True)
ws = wb.create_sheet()
ws.append(list(data))  # write header
for row in data.maps[:-1]:
    ws.append([row.get(key, "") for key in data])
wb.save(join(xml_root, "data.xlsx"))
🌐
Aspose
products.aspose.com › aspose.cells › python via .net › conversion › xml to excel
Python XML to EXCEL - XML to EXCEL Converter | products.aspose.com
November 13, 2025 - Add a library reference (import the library) to your Python project. Load XML file with an instance of Workbook. Convert XML to EXCEL by calling Workbook.save method.
🌐
Reddit
reddit.com › r/learnpython › how to parse xml into an excel sheet?
r/learnpython on Reddit: How to parse XML into an excel sheet?
December 2, 2021 -

Bare with me, as I'm a novice with python, but basically, I am trying to take an XML file, and plop it into an existing excel workbook in a specific sheet. I know I have done this successfully before, but cannot find the file where I did, nor can I remember how I did.

When I do it manually, the process is pretty straight forward - download the XML file, open it with excel, copy and paste as text into the sheet. Just hoping someone could help me get started here. Thanks so much for your time.

To be more specific this is the layout of the XML file:

<products>

<product active="1" on_sale="0" discountable="1">

<sku>GG1234</sku>

<name><![CDATA[ Product Name Here ]]></name>

<description><![CDATA[Product Description Here ]]></description>

<keywords></keywords>

<price>8.9</price>

<stock_quantity>220</stock_quantity>

<reorder_quantity>0</reorder_quantity>

<height>4.25</height>

<length>1.25</length>

<diameter>2.5</diameter>

<weight>0.53</weight>

<color></color>

<material>Material Here/material>

<barcode>0000000000</barcode>

<release_date>2010-02-19</release_date>

<images>

<image>/path/path.jpg</image>

<image>/path/path.jpg</image>

<image>/path/path.jpg</image>

<image>/path/path.jpg</image>

</images>

<categories>

<category code="518" video="0" parent="0">Category 1</category>

<category code="525" video="0" parent="528">Category 2</category>

<category code="138" video="0" parent="0">Category 3</category>

<category code="552" video="0" parent="528">Category 4</category>

</categories>

<manufacturer code="AC" video="0">Manufact</manufacturer>

<type code="CL" video="0">Product Type</type>

</product> . . . . .

<products>

What I need is for the follow values to populate the top row as the header of the excel file:

active
on_sale
disctountable
sku
name
description
keywords
price
stock_quantity
reorder_quantity
height
length
diameter
weight
color
material
barcode
release_date
image
category
manufacturer
code2
video3
type
code4
video5

And then their respective values to populate the cells going downward in the columns.

Hope that makes sense

🌐
GitHub
github.com › vrushabhkaushik › XML-to-Excel-Conversion
GitHub - vrushabhkaushik/XML-to-Excel-Conversion: The python script reads the XML file name from user's input, and then using data frame, it writes the data to an Excel sheet
The python script reads the XML file name from user's input, and then using data frame, it writes the data to an Excel sheet - vrushabhkaushik/XML-to-Excel-Conversion
Starred by 5 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
Aspose
blog.aspose.com › aspose blog › convert xml to excel in python
Convert XML to Excel Python | Export XML to Excel in Python
March 15, 2024 - Convert XML to Excel in Python. Export data from an XML file to Excel programmatically in Python using Aspose.Cells for Python API.
Find elsewhere
🌐
e-iceblue
e-iceblue.com › Tutorials › Python › Spire.XLS-for-Python › Program-Guide › Conversion › Python-Convert-XML-to-Excel-and-XML-to-PDF.html
Python: Convert XML to Excel and XML to PDF
Learn how to convert XML to Excel and XML to PDF using Spire.XLS for Python. Master the process with step-by-step instructions and tips for efficient data transformation.
🌐
Like Geeks
likegeeks.com › home › python › pandas › export xml to excel using python pandas
Export XML to Excel using Python Pandas
April 27, 2025 - import pandas as pd data = pd.read_xml('customers_plans.xml') filtered_data = data[data['Plan'] == 'Gold'] filtered_data.to_excel('gold_plan_customers.xlsx', index=False) Try our converter now: parse intricate XML schemas, preserve nested elements, ...
🌐
Medium
medium.com › @soderholm.conny › how-to-generate-an-xml-file-from-excel-with-python-96cff7b768db
How to generate an XML file from Excel with Python | by Soderholm Conny | Medium
August 1, 2023 - We use a list comprehension to get all the values from the cells. Next, we are adding the babies. Notice the use of the With tag and text. When we are finished we indent our result with Yattags indent method.
🌐
freeCodeCamp
forum.freecodecamp.org › python
Convert Excel XML to .xlsx with python - Python - The freeCodeCamp Forum
September 29, 2020 - Hello guys, so i have this XML file which i can open pretty easily using Excel this is the file, but i want to convert it to xlsx or any compatible format to be able to use openpyxl module to it so that i can read it eas…
🌐
Python.org
discuss.python.org › python help
Can Python read/write XML and Excel XLSX files? - Python Help - Discussions on Python.org
January 9, 2024 - I did 15 minutes of a Python tutorial and it looks very similar to Perl. Thank you for your time! ... For xlsx see here. For XML see xlml or lxml. dancergraham (Graham Knapp) January 9, 2024, 1:30pm ... You can also use the pandas library if you are dealing with tabular data - it has good support for excel import and export, it is reasonably quick and flexible for handling bulk operations on columns of data and it can handle some simple custom xml formats IO tools (text, CSV, HDF5, …) — pandas 2.1.4 documentation
🌐
ProjectPro
projectpro.io › recipes › convert-excel-document-xml-format
How to convert an Excel document to XML format? -
May 11, 2022 - XML is considered to be at odds with relational schemas, which puts it in a camp with most of NoSQL technology · Install Yattag python module as follows: pip install yattag · Install OpenPyXL python module as follows:pip install openpyxl · The below codes can be run in Jupyter notebook , or any python console · In this example we are going to use the excel ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-convert-excel-to-xml-format-in-python
How to Convert Excel to XML Format in Python? | GeeksforGeeks
March 21, 2024 - JSON (JavaScript Object Notation) is a widely used data format that is both easy to read and write for humans and simple for machines to parse and generate. However, there may be times when we need to convert this JSON data into an Excel spreadsheet for analysis or reporting. In this article, we'll ... In this article, the task is to convert a given list to XML in Python.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-xml-to-csv-in-python
Convert XML to CSV in Python - GeeksforGeeks
July 23, 2025 - We used ElementTree to parse and navigate through the XML structure. Data from each record was collected into a list of dictionaries. Finally, we used pandas to create a CSV file from that structured data.
🌐
Adimian
adimian.com › blog › fast-xlsx-parsing-with-python
Fast Xlsx Parsing With Python - Adimian: Sustainable Python Software Development & Consultancy
January 2, 2024 - There is no easy way to extract information as-is, hence the raw representation has to be post-processed, that is why Python suffers when parsing XLSX. Now that we have collected the workbook and shared string data, we can move our attention to the sheets themselves. This is where performance matters because those are the parts that growth when sheets content gets larger. For our example, xl/worksheets/sheet1.xml looks like this:
🌐
Aspose
products.aspose.com › aspose.cells › python via java › merger › merge-xml to excel
Merge XML to EXCEL - Combine XML to EXCEL in Python | products.aspose.com
November 13, 2025 - Python Excel API not only convert between spreadsheet formats, it can also render Excel files as images, PDF, HTML, ODS, CSV, SVG, JSON, WORD, PPT and more, thus making it a perfect choice to exchange documents in industry-standard formats. You may install Aspose.Cells for Python via Java from pypi, use command as: $ pip install aspose-cells. The following example demonstrates how to merge XML to EXCEL in Aspose.Cells for Python via Java.