Working with Excel data in Python is common. It often involves moving data from Excel into a form that can be manipulated efficiently. Converting Excel data into a format ready for analysis can be tricky. In this blog post, you will learn how to convert Excel to NumPy arrays in just a few lines of code.
Why NumPy?
NumPy (Numerical Python) is an open-source Python library. It is the backbone of data science and machine learning in Python. NumPy provides fast array operations and efficient numerical computing. It works smoothly with pandas, TensorFlow, and scikit-learn. The library supports multidimensional arrays, matrices, linear algebra, and Fourier transforms. Arrays use contiguous memory, making them faster than Python lists. This speed makes NumPy a core tool for scientific computing and data analysis.
Excel is widely used for storing datasets, but it is not optimized for Python workflows. Traditional conversion methods often require extra libraries, multiple steps, and manual parsing. Aspose.Cells allows you to export data from Excel, TSV, CSV, and JSON formats directly into NumPy arrays. This connects spreadsheets with Python’s numerical tools.
What is Aspose.Cells for Python?
Aspose.Cells is the best Excel library for Python developers. It allows reading, creating, and manipulating spreadsheets without relying on Microsoft Excel. The Python via .NET variant embeds the .NET version of Aspose.Cells and exposes it to Python. Aspose.Cells simplifies the process of converting Excel to NumPy. It lets you export an entire workbook, worksheet, range, row, column, or even a list object directly into NumPy ndarrays. This means you can move from raw Excel files to clean, ready-to-use data for analysis or machine learning with minimal effort.
You can install it from PyPI:
pip install aspose‑cells‑python
Once installed, import the library alongside NumPy:
import aspose.cells as cells
import numpy as np
How to Convert Excel Workbook to NumPy
A workbook may contain multiple worksheets. You can export an entire Excel workbook into a NumPy ndarray in one go. This is handy when you want to process data from all sheets directly in Python.
Follow the steps below to convert an Excel workbook to a NumPy ndarray:
- Load the Excel workbook using the
Workbook
class. - Access all worksheets from the workbook.
- Loop through each worksheet to read its used rows and columns.
- Extract cell values row by row.
- Store each sheet’s data into a list of lists.
- Convert the collected data into a NumPy ndarray using np.asarray().
Here is a simplified Python script that exports a sample workbook:

How to Convert Excel Workbook to NumPy
This script replaces any blank cells with empty strings and combines all worksheets into one NumPy array. The final excel_array is three-dimensional: the first level represents sheets, the second represents rows, and the third represents columns.
[[['City', 'Region', 'Store'],
['Chicago', 'Central', '3055'],
['New York', 'East', '3036'],
['Detroit', 'Central', '3074']],
[['City2', 'Region2', 'Store3'],
['Seattle', 'West', '3000'],
['philadelph', 'East', '3082'],
['Detroit', 'Central', '3074']],
[['City3', 'Region3', 'Store3'],
['Seattle', 'West', '3166'],
['New York', 'East', '3090'],
['Chicago', 'Central', '3055']]]
Converting a Single Worksheet to NumPy
Sometimes, you may want to work with a single worksheet instead of an entire workbook. You can directly extract the cell values of one worksheet and convert them into a NumPy ndarray by following the steps below:
- Load the Excel file with the
Workbook
class. - Access the target worksheet by its index.
- Get the maximum used rows and columns.
- Loop through each row and column to collect cell values.
- Store the extracted data in a list.
- Convert the list to a NumPy ndarray with np.asarray().
Here is the Python script that exports a single worksheet:
This creates a 2D ndarray where rows map to Excel rows and columns map to Excel columns.
[['City' 'Region' 'Store']
['Chicago' 'Central' '3055']
['New York' 'East' '3036']
['Detroit' 'Central' '3074']]
How to Convert a Range of Excel to NumPy
In certain cases, you only need a specific range of cells. Aspose.Cells lets you define a range and export it directly to a NumPy ndarray.
Follow the steps below:
- Load the workbook with the
Workbook
class. - Select the target worksheet.
- Define a range using the
worksheet.cells.create_range()
method. - Loop through the rows and columns of the range to extract values.
- Convert the values into a NumPy ndarray using np.asarray().
The following code example shows how to convert a range of cells from Excel to NumPy ndarray:
If the selected range covers two columns and three rows, the resulting array will be 3×2, such as:
[['City' 'Region']
['Chicago' 'Central']
['New York' 'East']]
Converting an Excel Table (ListObject) to NumPy
An Excel Table is a structured range of data with headers and rows. In Aspose.Cells, this is represented as a ListObject. You can easily export the contents of an Excel Table into a NumPy ndarray for further processing in Python.
- Load the workbook and select the worksheet.
- Access the ListObject (Excel Table) from the worksheet.
- Export the table’s data into a two-dimensional array.
- Convert the array into a NumPy ndarray.
- Use the ndarray for data science or machine learning workflows.
The following code example shows how to export an Excel table (ListObject) to NumPy:
The resulting NumPy ndarray will contain the rows and columns of the Excel Table, including headers if they are part of the data range.
[['City', 'Region', 'Store'],
['Chicago', 'Central', '3055'],
['New York', 'East', '3036'],
['Detroit', 'Central', '3074']]
How to Convert a Row of Excel to NumPy
Sometimes you only need data from a single row in Excel. Aspose.Cells makes it simple to extract one row and load it directly into a NumPy ndarray.
- Load the Excel workbook.
- Select the worksheet.
- Choose the row index you want to export.
- Export the row values as an array.
- Convert the array into a NumPy ndarray for processing.
The following Python code shows how to convert a row of an Excel sheet to NumPy ndarray:
The resulting NumPy ndarray will be a one-dimensional array containing all values from the selected row.
['Detroit' 'Central' 3074]
Convert a Column of Excel to NumPy
In some cases, you may only need values from a single column of an Excel sheet. Aspose.Cells lets you export a column easily and transform it into a NumPy ndarray.
- Load the Excel workbook.
- Select the target worksheet.
- Choose the column index to export.
- Export the column values.
- Convert the values into a NumPy ndarray.
The following Python code shows how to convert a column of an Excel sheet to NumPy ndarray:
The resulting NumPy ndarray will be a one-dimensional array containing all values from the selected column.
['Store' 3055 3036 3074]
Tips for Working with Aspose.Cells and NumPy
Memory considerations: Converting very large workbooks into NumPy arrays can consume significant memory. Process worksheets individually or read specific ranges if possible.
Data types: If your spreadsheet contains mixed types (strings, numbers, dates), specify dtype=object when converting lists to NumPy arrays. For homogeneous numeric data you can let NumPy infer the type.
Missing values: Aspose.Cells returns None for empty cells. In the examples above we replaced them with empty strings. You can also substitute np.nan or another sentinel value depending on your use case.
Get a Free License
Do you want to explore the full power of Aspose.Cells for Python? You can request a free temporary license. This allows you to test all features without limitations or evaluation watermarks.
With a temporary license, you can:
- Work with large Excel files.
- Apply advanced formatting and styling.
- Perform conversions (e.g., Excel to PDF, NumPy, and more).
It is the best way to evaluate performance and compatibility with your projects before making a purchase decision.
Useful Resources
Here are some valuable resources to help you get started with Aspose.Cells for Python via .NET:
Conclusion
Aspose.Cells for Python via .NET simplifies the conversion of Excel data into NumPy arrays. Whether you need the entire workbook, a single sheet, a specific range, a table, a row or a column, the library provides clear methods to iterate through cells and build lists that NumPy can consume. By combining Aspose.Cells’ ability to read many spreadsheet formats with NumPy’s numerical power, you can integrate Excel data seamlessly into your Python data pipelines.
If you have got any questions, please feel free to ask at our free support forum, and we will be happy to help.