Spire.XLS for Python
Spire.XLS for Python is a standalone Excel API designed for processing Excel files (XLS, XLSX, XLSB, XLSM, CSV) within Python applications. It allows developers to create, read, write, convert, and print Excel documents without requiring Microsoft Office. The current version is 16.4.0, and it maintains a regular release cadence with multiple updates annually.
Common errors
-
ModuleNotFoundError: No module named 'spire'
cause The `spire-xls` package is not installed in the active Python environment or the environment is not correctly activated.fixInstall the package using `pip install spire-xls` in your project's virtual environment or global environment. -
System.ArgumentException: The free version of Spire.XLS for Python has a limitation of 500 rows and 5 worksheets for opening and writing.
cause You are attempting to process an Excel file that exceeds the row or worksheet limits imposed by the free evaluation version of Spire.XLS for Python.fixEither reduce the size/complexity of the Excel files you are processing to fit within the 500-row/5-worksheet limit, or acquire and activate a commercial license from e-iceblue. -
AttributeError: 'Worksheet' object has no attribute 'Range'
cause You are attempting to call `Range` as a method (e.g., `sheet.Range("A1")`) when it is an indexer property.fixAccess cells using bracket notation for the `Range` property, e.g., `sheet.Range["A1"].Value = "data"`. -
System.ArgumentException: SaveFormat.Xls does not match the file extension .xlsx.
cause There is a mismatch between the `SaveFormat` enumeration value provided to `SaveToFile` and the file extension specified in the output file path.fixEnsure the `SaveFormat` enum matches the file extension. Use `SaveFormat.Xlsx` for `.xlsx` files and `SaveFormat.Xls` for `.xls` files.
Warnings
- breaking The free evaluation version of Spire.XLS for Python has significant limitations. It restricts operations to a maximum of 500 rows and 5 worksheets per Excel file (for both loading and saving). Exceeding these limits will raise an exception.
- gotcha Processing very large Excel files (e.g., hundreds of thousands of rows or complex formatting) can lead to high memory consumption, as the library often loads the entire document into memory.
- gotcha While `sheet.Range["A1"]` uses Excel's 1-based A1 notation, direct access to collection elements like `sheet.Worksheets`, `sheet.Rows`, and `sheet.Columns` uses 0-based indexing common in Python.
Install
-
pip install spire-xls
Imports
- Workbook
from spire.xls import Workbook
- FileFormat
from spire.xls import FileFormat
- SaveFormat
from spire.xls import SaveFormat
Quickstart
from spire.xls import Workbook, SaveFormat
# Create a new workbook
workbook = Workbook()
# Access the first worksheet (0-indexed)
sheet = workbook.Worksheets[0]
# Write data to cells using A1 notation
sheet.Range["A1"].Value = "Hello, Spire.XLS!"
sheet.Range["B1"].Value2 = 12345
# Auto-fit columns
sheet.AllocatedRange.AutoFitColumns()
# Define the output file path
output_file = "output_spire_xls_quickstart.xlsx"
# Save the workbook in XLSX format
workbook.SaveToFile(output_file, SaveFormat.Xlsx)
workbook.Dispose()
print(f"Excel file saved to {output_file}")