Reading cell data, embedded images, and chart elements from Excel files is a common requirement in Python-based backend processing, automated report analysis, and data migration. Compared with conventional Excel processing libraries, the component introduced in this article provides a unified way to read text and numeric values, handle different cell data types, extract images, and export charts without requiring Microsoft Office or another desktop Office environment. This makes it suitable for server-side and automated processing scenarios.

In this article, we will use practical Python code examples to demonstrate four core Excel parsing capabilities and explain the key implementation details behind each approach.
The examples are based on Free Spire.XLS for Python , which provides a lightweight API for reading and writing Excel files. It supports parsing common elements in .xlsx files and offers a simple API with relatively low resource overhead, making it suitable for automated Excel data processing.
1. Install the Required Component
Free Spire.XLS for Python can be installed directly with pip and integrated into a Python environment without additional system-level configuration. The following command installs the package:
Install with pip
pip install spire.xls.free
Installation Notes
- The package can be installed directly with
pipwithout manually configuring additional system dependencies. - If multiple Python versions are installed on your system, use
pip3orpython -m pipas appropriate to ensure that the package is installed in the environment used by your application. - Once installation is complete, import the library with
import spire.xls. No manual environment-variable configuration is required.
After installation, you can use the APIs described below to read Excel data, extract embedded images, and export charts.
2. Read Cell Data from a Worksheet
For common Excel data-processing tasks, you can obtain the worksheet's allocated data range and then iterate through its rows and columns to read cell values in batches. Because this approach only processes the area occupied by data, it avoids unnecessarily scanning the entire worksheet and is suitable for automated table-data extraction.
Code Example
from spire.xls import *
# Create a workbook and load the Excel file
wb = Workbook()
wb.LoadFromFile("Input.xlsx")
# Get the first worksheet
sheet = wb.Worksheets[0]
# Get the allocated data range
locatedRange = sheet.AllocatedRange
# Iterate through all cells and print their values
for i in range(len(locatedRange.Rows)):
for j in range(len(locatedRange.Rows[i].Columns)):
print(locatedRange[i + 1, j + 1].Value + " ", end='')
print("")
Key Details
- The
AllocatedRangeproperty automatically identifies the used range of the worksheet, avoiding the overhead of iterating through unused rows and columns. - Cell indexing starts at 1, which is consistent with Excel's row and column numbering and eliminates the need for additional index conversion.
- The nested loops iterate through the rows and columns of the allocated range and retrieve each cell's value while preserving the original table structure.
3. Read Different Types of Cell Data
Excel cells can contain text, numbers, formulas, dates, Boolean values, and other types of data. Using the general Value property alone may not always provide enough control when you need to distinguish between these data types. Free Spire.XLS provides dedicated properties for retrieving specific types of cell content.
Code Example
# Get a cell by row and column
cell = sheet.Range[row, column]
# Read different types of cell data
text = cell.Text # Text content
number = cell.NumberValue # Numeric value
formula = cell.Formula # Original formula
formulaResult = cell.FormulaValue # Calculated formula result
date = cell.DateTimeValue # Date and time value
boolean = cell.BooleanValue # Boolean value
value = cell.Value # General value
value2 = cell.Value2 # Object-based value
Key Details
- For formula cells,
FormulaandFormulaValueallow you to retrieve the original formula and its calculated result separately. This is useful when you need to validate formulas or process their results. -
DateTimeValueprovides the date and time represented by a cell in a form that can be processed directly, avoiding manual string parsing. -
Value2returns the cell value as an object, preserving its underlying data type. For example, date and Boolean cells can be returned asdatetimeandboolobjects, respectively.
Using these dedicated properties gives you more precise control over how different types of Excel data are handled in your application.
4. Extract Embedded Images from a Worksheet
Excel worksheets often contain embedded images such as screenshots, product images, receipts, and other business-related resources. While some Excel libraries focus primarily on tabular data, Free Spire.XLS provides access to the images embedded in a worksheet.
You can iterate through the worksheet's picture collection and save each image as a standard PNG file for further processing or local storage.
Code Example
from spire.xls import *
workbook = Workbook()
workbook.LoadFromFile("Input.xlsx")
sheet = workbook.Worksheets[0]
# Iterate through all pictures in reverse order and save them
for i in range(sheet.Pictures.Count - 1, -1, -1):
pic = sheet.Pictures[i]
pic.Picture.Save(
"ExtractImages\\Image-{0:d}.png".format(i),
ImageFormat.get_Png()
)
Key Details
- The picture collection is traversed in reverse order, which can help avoid index-related issues when working with collections that may change during processing.
- You can customize the output directory and file naming convention. The example saves the extracted images as PNG files.
-
Pictures.Countreturns the number of pictures contained in the worksheet, allowing you to determine how many embedded images need to be processed.
This approach is particularly useful when Excel files are used as containers for both structured data and image-based content.
5. Export Excel Charts as Images
Excel files can contain various types of charts, including column charts, line charts, and pie charts. Chart elements cannot be retrieved through ordinary cell-value APIs. Instead, they can be rendered as images and exported for offline use, reporting, or further processing.
Free Spire.XLS provides the SaveChartAsImage method, which can be used to obtain chart images from a worksheet.
Code Example
from spire.xls import *
workbook = Workbook()
workbook.LoadFromFile("Input.xlsx")
sheet = workbook.Worksheets[0]
# Convert all charts in the worksheet to image streams
image_streams = workbook.SaveChartAsImage(sheet)
# Save the chart images
for i, image_stream in enumerate(image_streams):
image_stream.Save(f"Output/chart-{i}.png")
# Release workbook resources
workbook.Dispose()
Key Details
-
SaveChartAsImageretrieves the charts in the specified worksheet as image streams, so you do not need to locate and process each chart individually. - Saving the results through image streams provides a convenient way to handle multiple charts and export them as image files.
-
Dispose()explicitly releases the resources associated with the workbook. This is especially important when processing a large number of Excel files to reduce the risk of excessive memory usage and file-locking issues.
6. Summary
In this article, we explored four common Excel parsing tasks with Python:
- Reading cell data from a worksheet
- Retrieving different types of cell values
- Extracting embedded images
- Exporting Excel charts as images
The approach does not require Microsoft Office to be installed and can therefore be integrated into server-side scripts, automated data-processing workflows, data synchronization tools, and report-processing systems.
The main advantage of this approach is that it provides a relatively lightweight API for handling both structured worksheet data and embedded visual elements. With support for multiple cell data types, image extraction, chart export, and explicit resource management, it can be used as part of automated workflows that process Excel files in batches.
Top comments (0)