One way to achieve this is to use XSLT Transformation. Most programming languages including Python will have support to convert an XML document into another document (e.g. HTML) when supplied with an XSL.

A good tutorial on XSLT Transformation can be found here

Use of Python to achieve transformation (once an XSL is prepared) is described here

Answer from Gro on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › parsing-and-converting-html-documents-to-xml-format-using-python
Parsing and converting HTML documents to XML format using Python - GeeksforGeeks
July 23, 2025 - In this approach, we will use Python's lxml library to parse the HTML document and write it to an encoded string representation of the XML tree.The lxml XML toolkit is a Pythonic binding for the C libraries libxml2 and libxslt.
Discussions

Parsing HTML with the XML module
I need to parse HTML files with the Python 3.8 xml package. This must be possible because some of the xml.etree.ElementTree methods have parameters that take "xml" or "html" as a value, but I can’t find an example of how… More on discuss.python.org
🌐 discuss.python.org
0
0
November 16, 2021
Converting .txt with RGB values to .xml or .qml?
Trying to convert a .txt to a .qml for use as a style for standard USGS lithologies. Any help would be appreciated :) More on reddit.com
🌐 r/QGIS
4
0
October 4, 2020
Pandas dataframe to nested xml
xml is part of the standard library. You have a nice column name convention and we could think about being smarter using the dot to automatically work out the parent though best to manually to put it together per row using the apply function import io import xml.etree.ElementTree as ET import pandas as pd def build_item_xml(row): item1 = ET.SubElement(items, 'Item') descriptors = ET.SubElement(item1, 'Descriptors') barcode= ET.SubElement(descriptors, 'Barcode') barcode.text=row["Descriptors.Barcode"] pricing = ET.SubElement(item1, 'Pricing') packetcost= ET.SubElement(pricing, 'PackCost') packetcost.text=str(row["Pricing.PackCost"]) # cast as without error cannot serialize 0.5625 (type float) # etc # add other attributes here # always return a result return row # mock dataframe with 2 rows based on columns supplied df = pd.DataFrame({ "Descriptors.Barcode": ["9770307017919", "9770307017920"], "Descriptors.SupplierCode": ["030701791", "030701792"], "Descriptors.Description": ["Daily Express (Mon)", "Daily Express (Tues)"], "Descriptors.CommodityGroup": [1,2], "Pricing.PackCost": [0.5625, 0.5626], "Pricing.CostPricePerUnit": [0.5625, 0.5626], "Pricing.RetailPrice": [0.75, 0.75], "Pricing.ValidFrom": [44193, 44194], "Sizing.Packsize": [1, 2], }) # https://docs.python.org/3/library/xml.etree.elementtree.html#building-xml-documents import xml.etree.ElementTree as ET items = ET.Element('Items') df = df.apply(build_item_xml, axis=1). # this calls build_item_xml per row ET.dump(items) More on reddit.com
🌐 r/learnpython
3
1
December 29, 2020
How to parse a XML file into a kotlin Data Class
🌐 r/Kotlin
14
17
March 3, 2019
🌐
lxml
lxml.de › parsing.html
Parsing XML and HTML with lxml
Otherwise, you can resort at runtime to other (usually slower) Python tools for passing decompressed input into lxml or reading from the network. If you want to parse from a string (bytes or text) and still provide a base URL for the document (e.g. to support relative paths in an XInclude), you can pass the base_url keyword argument: >>> root = etree.fromstring(xml, base_url="http://where.it/is/from.xml")
🌐
Aspose
products.aspose.com › aspose.slides › python via .net › conversion › html to xml
Convert HTML to XML in Python | products.aspose.com
July 24, 2025 - Add a library reference (import the library) to your Python project. Open the source HTML files in Python. Save result as XML file. You can also convert HTML and save to other file formats.
🌐
Aspose
products.aspose.com › aspose.cells › python via java › conversion › html to xml
Convert HTML to XML in Python Excel Library
November 13, 2025 - Add a library reference (import the library) to your Python project. Load HTML file with an instance of Workbook. Convert HTML to XML by calling Workbook.save method.
🌐
Barbaresi
adrien.barbaresi.eu › blog › parsing-converting-lxml-html-tei.html
Parsing and converting HTML documents to XML format using Python’s lxml - Bits of Language: corpus linguistics, NLP and text analytics
December 22, 2015 - The more pythonic way would be to make sure all the necessary libraries are installed (something like apt-get install libxml2-dev libxslt1-dev python-dev), and then using a package manager such as pip: pip install lxml. Here are the modules required for basic manipulation: from __future__ import print_function from lxml import etree, html from StringIO import StringIO
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › parsing-and-converting-html-documents-to-xml-format-using-python
Parsing and Converting HTML Documents to XML format using Python
July 25, 2023 - We must map the HTML elements to the XML elements in order to convert HTML to XML while making sure that the structure and content are appropriately reflected. To generate XML elements, set properties, and construct the XML tree structure, we may utilize Python's XML libraries, such as xml.etree.ElementTree or lxml.etree.
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - Writes the element tree to a file, as XML. file is a file name, or a file object opened for writing. encoding [1] is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 or Unicode (default is None). default_namespace sets the default XML namespace (for “xmlns”). method is either "xml", "html" or "text" (default is "xml").
🌐
Example Code
example-code.com › python › htmlToXml_convertFile.asp
CkPython Convert HTML File to XML File
Chilkat • HOME • .NET Core C# • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi ActiveX • Delphi DLL • Go • Java • Lianja • Mono C# • Node.js • Objective-C • PHP ActiveX • PHP Extension • Perl • PowerBuilder • PowerShell ...
🌐
Python Forum
python-forum.io › thread-21577.html
HTML to XML
October 5, 2019 - I'm trying to convert HTML to XML. Then load the XML into a database for data manipulation. I researched and came to the conclusion that I can use Python to do this. If I'm right I will start learning
🌐
Hurix Digital
hurix.com › blogs › how-to-convert-html-to-xml-efficiently
How to Convert HTML to XML Efficiently?
November 17, 2025 - If you have programming experience, you can write a script to convert HTML to XML. Python, for example, has several libraries such as BeautifulSoup and lxml that can be used for parsing and transforming HTML into XML.
🌐
PyPI
pypi.org › project › html2py
html2py · PyPI
October 11, 2018 - - wrap python code in a function and add imports. - A command line interface for generating script. - Also an API for extend functionalities, see module documenation for more details. ## Dependencies - lxml - yattag (for running generated scripts) #### For testing: - hypothesis - pytest (or something that fetch test cases in modules) - xmldiff ## Usage #### Command line tool ``` usage: html2py.py [-h] -o OUTPUT [-e | -f | -c] [-s] file Convert html file to yattag based python script.
      » pip install html2py
    
Published   Oct 11, 2018
Version   1.0.1
🌐
Python.org
discuss.python.org › python help
Parsing HTML with the XML module - Python Help - Discussions on Python.org
November 16, 2021 - I need to parse HTML files with the Python 3.8 xml package. This must be possible because some of the xml.etree.ElementTree methods have parameters that take "xml" or "html" as a value, but I can’t find an example of how it’s done. I get an exception when I try to parse the HTML file: htmlRoot = etree.ElementTree.parse(filepathname).getroot() The parser throws an “undefined entity” exception when it encounters an HTML entity.
🌐
GitHub
github.com › Chadii › From-xml-to-html-python-script
GitHub - Chadii/From-xml-to-html-python-script: This is a small useful script that convert multiple output xml scan done with nmap with the command nmap ip -oX name.xml to html files. Use : Be sure to name the xml files as a number , for example : 1.xml , 25.xml ... and have the script in the same xml files folder , or change the path in the script .Video =>
This is a small useful script that convert multiple output xml scan done with nmap with the command nmap ip -oX name.xml to html files. Use : Be sure to name the xml files as a number , for example : 1.xml , 25.xml ... and have the script in ...
Starred by 5 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
Sonra
sonra.io › home › xml › xml conversion using python in 2025
XML Conversion Using Python in 2026 - Sonra
January 14, 2025 - Python provides several libraries and methods suitable for XML conversion, catering to various data handling requirements. While XML is one of many formats used for data exchange and storage, its conversion into more commonly used formats like CSV can enhance data interoperability and integration. This overview introduces key Python libraries, including lxml, ElementTree, xmltodict, BeautifulSoup, and pandas, that facilitate the conversion process. These libraries offer diverse approaches for parsing, modifying, and converting XML.
🌐
Aspose
products.aspose.com › aspose.cells › python via .net › conversion › xml to html
Convert XML to HTML in Python Excel Library
November 13, 2025 - Need to convert XML files to HTML programmatically? Python developers can easily load & convert XML to HTML in just a few lines of code.
🌐
py
py.readthedocs.io › en › latest › xml.html
py.xml: simple pythonic xml/html file generation — py 1.11.1.dev5+g6b21973 documentation
On a side note, you’ll see that the unicode-serializer supports a nice indentation style which keeps your generated html readable, basically through emulating python’s white space significance by putting closing-tags rightmost and almost invisible at first glance :-) ... from py.xml import html # html namespace paras = "First Para", "Second para" doc = html.html( html.head( html.meta(name="Content-Type", value="text/html; charset=latin1")), html.body( [html.p(p) for p in paras])) print unicode(doc).encode('latin1')
🌐
O'Reilly
oreilly.com › library › view › python-xml › 0596001282 › ch03s06.html
Converting XML to HTML - Python & XML [Book]
December 18, 2001 - The PyXML package contains XML parsers, including PyExpat, as well as support for SAX and DOM, and much more. While learning the ropes of the PyXML package, it would be nice to have a comprehensive list of all the classes and methods. Since this is a programming book, it seems appropriate to write a Python program to extract the information we need—and in XML, no less!
Authors   Christopher A. JonesFred L. Drake
Published   2001
Pages   380