How to Convert Excel to XML Format in Python? Last Updated : 21 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Python proves to be a powerful language when the requirement is to convert a file from one format to the other. It supports tools that can be employed to easily achieve the functionality. In this article, we'll find out how we will convert from an Excel file to Extensible terminology (XML) files with Python. Modules neededOpenPyXL helps in interacting with Excel files. It can read and write to .xlsx and .xlsm files and can be installed as: pip install openpyxl Yattag is a Python library for generating HTML or XML documents with Python in a very readable way. This Yattag Library is pretty simple and easy to use library. If you are searching for any library in order to more easily generate HTML or XML documents. pip install yattag Function neededTo load the contents of the Excel file load_workbook() method of OpenPyXl is used.To iterate through loaded file and read data Iter_rows() with appropriate attributes is used Syntax: Iter_rows(min_col, min_row, max_col, max_row, values_only) Parameters: min_col (int) – smallest column value (1-based index)min_row (int) – smallest row value (1-based index)max_col (int) – largest column value (1-based index)Max_row (int) – largest row value (1-based index)values_only (bool) – whether only cell values should be returnedThe tagtext() method is a helper method that returns a triplet composed of:The Doc instance itselfThe tag method of the Doc instanceThe text method of the Doc instanceThe asis method appends a string to the document without any form of escaping.The tag method will accept any string as a tag name.The indent function takes a string representing an XML or HTML document and returns a well-indented version of this document. Database in use: Click here To convert Excel data to XML first, it needs to be read, the given program explains the mechanism for reading data. Approach Import moduleLoad Excel fileCreate sheet objectIterate through rows Example Python3 # Install the openpyxl library from openpyxl import load_workbook # Loading our Excel file wb = load_workbook("demo_database.xlsx") # creating the sheet 1 object ws = wb.worksheets[0] # Iterating rows for getting the values of each row for row in ws.iter_rows(min_row=1, max_row=2, min_col=1, max_col=6): print([cell.value for cell in row]) Now, Once we are done with Reading data. Let's Code how to convert Excel to XML format, Approach: Import moduleRead dataCreate XML format pageAppend to fileSave file Example: Python3 from openpyxl import load_workbook from yattag import Doc, indent # Load our Excel File wb = load_workbook("demo_database.xlsx") # Getting an object of active sheet 1 ws = wb.worksheets[0] # Returning returns a triplet doc, tag, text = Doc().tagtext() xml_header = '<?xml version="1.0" encoding="UTF-8"?>' xml_schema = '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema>' # Appends the String to document doc.asis(xml_header) doc.asis(xml_schema) with tag('People'): for row in ws.iter_rows(min_row=2, max_row=10, min_col=1, max_col=6): row = [cell.value for cell in row] with tag("Person"): with tag("First_Name"): text(row[0]) with tag("Last_Name"): text(row[1]) with tag("Gender"): text(row[2]) with tag("Country"): text(row[3]) with tag("Age"): text(row[4]) with tag("Date"): text(row[5]) result = indent( doc.getvalue(), indentation=' ', indent_text=True ) with open("output.xml", "w") as f: f.write(result) Output:output.xml Comment More infoAdvertise with us Next Article How to convert PDF file to Excel file using Python? S shubhanshuarya007 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-XML Practice Tags : python Similar Reads How to Convert JSON to Excel in Python 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 3 min read How to convert lists to XML in Python? In this article, the task is to convert a given list to XML in Python. But first, let's discuss what is an XML? It could also be terminology that defines a gaggle of rules for encoding documents during a format that's both human-readable and machine-readable. The design goals of XML specialize in si 3 min read How to Convert XML to CSV in R? Converting data from XML to CSV format can be a handy skill, especially when you need to work with data in a more accessible format like CSV. Here, we'll discuss about the process of converting XML files to CSV using R programming language for data analysis.What is XML (eXtensible Markup Language)?X 4 min read Convert XML to CSV in Python XML (Extensible Markup Language) is widely used to store and transport structured data. However, for tasks like data analysis or spreadsheet editing, CSV (Comma-Separated Values) is far more user-friendly and easier to work with.To make XML data easier to process, we often convert it to a CSV file. 2 min read How to convert PDF file to Excel file using Python? In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple 2 min read How to Convert XML to DataFrame in R? A Data Frame is a two-dimensional and tabular data structure in the R that is similar to a table in the database or an Excel spreadsheet. It is one of the most commonly used data structures for the data analysis in R with the columns representing the various and rows representing the observations.XM 4 min read Like