Convert Excel XLS to XLSX or XLSX to XLS with Python
Understand XLS and XLSX Formats
- XLS format: The XLS format is the older binary file format used by Excel versions prior to Excel 2007. It stores data, formulas, formatting, and other information in a binary structure. XLS files have a file extension of “.xls”.
- XLSX format: The XLSX format is the newer XML-based file format introduced in Excel 2007. It is an open standard and stores data, formulas, formatting, and other information in separate XML files within a ZIP archive. XLSX files have a file extension of “.xlsx”.
Why Convert Between XLS and XLSX Formats
- Support for newer features: XLSX format supports newer features and functionalities that may not be available in the older XLS format. By converting from XLS to XLSX, you gain access to advanced features such as enhanced formatting options, increased row and column limits, improved charting capabilities, support for modern Excel functions, and compatibility with newer Excel add-ins.
- Compatibility with older excel versions: XLS files are compatible with older versions of Excel, including Excel 97–2003. If you need to share files with individuals or organizations who are using these older versions of Excel, converting XLSX files to XLS format can ensure compatibility.
Python Library to Convert between XLS and XLSX Formats
To convert between Excel XLS and XLSX formats in Python, we can use the Spire.XLS for Python library. It is an easy-to-use and feature-rich library for creating, reading, editing, and converting Excel files within Python applications. With this library, you can work with a lot of spreadsheet formats, such as XLS, XLSX, XLSB, XLSM, and ODS. Moreover, you are also able to render Excel files to other types of file formats, such as PDF, HTML, CSV, Text, Image, XML, SVG, ODS, PostScript, and XPS.
You can install Spire.XLS for Python from pypi by running the following commands in your terminal:
pip install Spire.Xls
For more detailed information about the installation, you can check this official documentation: How to Install Spire.XLS for Python in VS Code.
Convert Excel XLS to XLSX in Python
The Spire.XLS for Python library provides the Workbook.SaveToFile(string filePath, ExcelVersion version) method, which enables seamless conversion between different Excel versions. By specifying the appropriate ExcelVersion parameter, such as ExcelVersion.Version2007 or ExcelVersion.Version2010, you can easily save XLS files to XLSX file format.
Here’s an example of how to convert an XLS file to XLSX format using Spire.XLS for Python:
from spire.xls import *
from spire.xls.common import *
#Create a Workbook instance
workbook = Workbook()
#Load an XLS file
workbook.LoadFromFile("Input.xls")
#Save the XLS file to an XLSX file
workbook.SaveToFile("ToXLSX.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
Convert Excel XLSX to XLS in Python
The steps and code to convert XLSX to XLS are almost the same as that of converting XLS to XLSX, the difference is that you need to specify the ExcelVersion parameter in the Workbook.SaveToFile(string filePath, ExcelVersion version) method as ExcelVersion.Version97to2003.
Here’s an example of how to convert an XLSX file to XLS format using Spire.XLS for Python:
from spire.xls import *
from spire.xls.common import *
#Create a Workbook instance
workbook = Workbook()
#Load an XLSX file
workbook.LoadFromFile("Input.xlsx")
#Save the XLSX file to an XLS file
workbook.SaveToFile("ToXLS.xls", ExcelVersion.Version97to2003)
workbook.Dispose()
Related Topics
Convert Excel to Images (PNG, JPEG, BMP) with Python
Python — How to Convert Excel XLS or XLSX to PDF
Convert Excel to CSV or CSV to Excel with Python (Simple Example)