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
🌐
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.
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"))
Discussions

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 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
November 26, 2021
Converting multisheet XML to Excel with python - Stack Overflow
I have hundreds of XML files I need to convert to XLSX for downstream analysis, but I have far too many to do by hand. I have been trying to figure out a python way to do this, and have found a num... More on stackoverflow.com
🌐 stackoverflow.com
July 14, 2025
What is the best OCR program to turn a .pdf into Excel data?

I've had to do something similar a few times in the past, and never found a good OCR solution.

What I ended up doing was opening the PDF in the full version of Adobe Acrobat (not reader), which has many file export options.

Depending on the contents and format, I'd either export it as an xls file, csv, or xml, and then open up the resulting file in excel, and do some magic with it (text to columns, string manipulation, occasional macros, etc).

The Adobe export would get the text about 90% exported correctly, with just a bit of manual manipulation needed.

More on reddit.com
🌐 r/datasets
13
17
September 20, 2012
🌐
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, map tags to columns, and customize output formatting. ... Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development.
🌐
PyPI
pypi.org › project › xml2xlsx
xml2xlsx · PyPI
Basic features of the library include creating multiple, named sheets within one workbook and creating rows of cells in these sheets. However, there are more possibiliteis to create complex excel based reports. Each cell can be specified to use one of the types: ... Type is defined in type cell attribute. The cell value is converted appropriately to the type specified.
      » pip install xml2xlsx
    
Published   Sep 16, 2024
Version   1.0.2
🌐
Aspose
products.aspose.com › aspose.cells › python via .net › conversion › xml to excel
Convert XML to EXCEL in Python Excel Library - Conversion
November 13, 2025 - Convert XML to EXCEL by calling Workbook.save method. Get the conversion result of XML to EXCEL. We host our Python packages in PyPi repositories. Install Aspose.Cells for Python from pypi, use command as: $ pip install aspose-cells-python.
🌐
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.blogs › 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
🌐
EasyXLS
easyxls.com › manual › tutorials › python › convert-xml-spreadsheet-to-excel.html
How to convert XML spreadsheet file to Excel in Python
Code sample Python: Convert XML Spreadsheet to Excel file in Python by EasyXLS library. XLSX, XLSM, XLSB, XLS in Python
🌐
Comfort Ajala
ajalacomfort.com › home › python 4 beginners: xml to excel within a minute
Python 4 Beginners: XML to Excel within a minute - Comfort Ajala
March 14, 2024 - Learn how to convert XML data to Excel spreadsheets quickly and easily with Python 4 Beginners. Master the process in just one minute with our step-by-step guide. Ideal for those new to programming and looking to enhance their skills. Start saving time and improving your data management today.
🌐
E-iceblue
cdn.e-iceblue.com › Tutorials › Python › Spire.XLS-for-Python › Program-Guide › Conversion › convert-xml-to-csv-in-python.html
How to Convert XML to CSV in Python: A Complete Guide
In this tutorial, we’ll explore how to efficiently transform XML files into CSV using Spire.XLS for Python. ... Before we can start, we need to install the Spire.XLS library. The package is available on PyPI , so installation is straightforward. Run the following command: ... Spire.XLS provides the Workbook and Worksheet objects for managing Excel-like files. You’ll use them to create new CSV files and populate them with your XML data.
🌐
CodePal
codepal.ai › code generator › convert xml to excel with nested elements
Convert XML to Excel with Nested Elements - CodePal
January 15, 2024 - convertxmltoexcel(xmlfile, excelfile) print(f”XML file ‘{xmlfile}’ converted to Excel file ‘{excel_file}’ successfully.”) ... Let us know if this guide helped you or if you have suggestions for improvement.
🌐
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?
November 26, 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

🌐
Quora
quora.com › How-can-I-convert-XML-to-Excel-using-Python-scripting
How to convert XML to Excel using Python scripting - Quora
Answer: Hi you can use regular expression to extract data. And then you can write the got results into the excel through the python script itself. Re module is used for extracting data and excel module to open and write into the excel file
🌐
GroupDocs Cloud
blog.groupdocs.cloud › groupdocs cloud blog › convert xml to excel and excel to xml in python
Convert XML to Excel and Excel to XML in Python
October 28, 2022 - Transforming extensive XML data into Excel can be accomplished online or by employing an online Excel to XML converter for Tally. This guide demonstrates the process of converting XML to Excel and Excel to XML using Python.
🌐
Aspose
kb.aspose.com › cells › python › how-to-convert-xml-to-excel-file-using-python
How to Convert XML to Excel File using Python
March 21, 2024 - First of all, create an instance of the Workbook class to load the source XML file. Next, invoke the save method to render the output Excel file while specifying the XLSX file format.
🌐
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. To learn about the pandas module in depth, refer to: Python Pandas Tutorial
🌐
Sonra
sonra.io › home › xml › xml to excel – complete guide to convert & import files
XML to Excel - Complete Guide to Convert & Import Files - Sonra
June 19, 2025 - By now, I’ve given you several options for XML to Excel converters, and it’s easy to get stuck picking the wrong one for the job. Some options are perfect for quick one-offs; others are designed for mission-critical pipelines with millions of rows. So, what should you use and when? Let’s run through the main options: The “File → Open” method in Excel. The Power Query method. The XML Maps method. Manual code (e.g., Python).