Python — Convert Excel to XML, or XML to Excel

Andrew Wilson
2 min readSep 25, 2024

--

The conversion between Excel and XML format involves transforming data from an Excel spreadsheet format into XML (eXtensible Markup Language), and vice versa. This process is essential for various applications, from data sharing to integration with web services and databases.

In this article, you will learn how to programmatically convert Excel to XML, or convert XML to Excel with Python.

Third-party Excel Library

Spire.XLS for Python is a Python library that supports various Excel document processing features, including creation, reading, writing, and conversion. The library can be directly installed to your system using the pip command below

pip install Spire.XLS

Or you can download the product package via below link and then installing it from a local path.

Convert Excel to XML in Python

  1. Initialize a Workbook instance.
  2. Load an Excel document using LoadFromFile() method.
  3. Save the Excel in XML format using SaveAsXML(fileName: str) method or SaveToFile(fileName: str, FileFormat.XML) method.

Python code:

from spire.xls import *
from spire.xls.common import *

# Load an Excel file
workbook = Workbook()
workbook.LoadFromFile("Inventories.xlsx")

# Save the Excel in XML format
workbook.SaveAsXml("XLSXtoXML.xml")
# workbook.SaveToFile("XLSXtoXML.xml", FileFormat.XML)
workbook.Dispose()
Python Excel to XML

Convert XML to Excel in Python

  1. Initialize a Workbook instance.
  2. Load an Xml file using LoadFromXml() method.
  3. Save the Xml file in XLSX or XLS format using SaveToFile(fileName: str, FileFormat.Version2016) method or SaveToFile(fileName: str, FileFormat.Version97to2003) method.

Python code:

from spire.xls import *
from spire.xls.common import *

# Load an XML file
workbook = Workbook()
workbook.LoadFromXml("XLSXtoXML.xml")

# Save the XML file in XLSX or XLS format
workbook.SaveToFile("XMLtoXLSX.xlsx", FileFormat.Version2016)
workbook.SaveToFile("XMLtoXLS.xls", FileFormat.Version97to2003)
workbook.Dispose()
Python Xml to Excel

--

--

Andrew Wilson
Andrew Wilson

Written by Andrew Wilson

Explore C#, Java and Python solutions for processing Word/Excel/PowerPoint/PDF files.

No responses yet