MS Excel provides a convenient way of keeping and sharing data in the form of rows and columns. More often, Excel files are used to store huge datasets having hundreds and thousands of records. While working with Excel files in Python, you may need to read data from each cell in the worksheets. To achieve that, this article shows how to read an Excel file in Python. You will learn how to read data from a single worksheet or all the worksheets in an Excel workbook.
Python Library to Read Excel Files
To read data from the workbooks, we will use Aspose.Cells for Python via Java. It is a powerful and feature-rich Python library to create MS Excel files. Moreover, it allows you to read and manipulate existing Excel files seamlessly. You can download the library or install it using the following pip command.
pip install aspose-cells
Read an Excel File in Python
Before we start reading the data, let’s have an overview of how the data is managed in an Excel file. An Excel file is termed as workbook that acts as a container. Each workbook contains one or more worksheets and every worksheet is composed of a number of cells. These cells are uniquely identified by the rows and columns. So, in order to read data from a cell, you need to know its row and column index.
The following are the steps to read an Excel file and print its data in Python.
- Load the Excel file using Workbook class.
- Get reference of the WorksheetCollection using Workbook.getWorksheets() method.
- Loop through worksheets in the collection and in each iteration, perform following steps:
- Get reference of the worksheet in an object.
- Get count of data rows and columns in the worksheet.
- Start a loop for rows.
- Start a nested loop for columns.
- Read data from each cell using Worksheet.getCells().get(rowIndex, columnIndex).getValue() method.
The following code sample shows how to read an Excel file in Python.
The following is the output we get after running the code sample above.
Read a Particular Worksheet in Excel in Python
You can also read a particular worksheet in the Excel file by following the steps below.
- Load the Excel file using Workbook class.
- Get reference of desired worksheet using Workbook.getWorksheets().get(index) method.
- Get count of data rows and columns in the worksheet.
- Start a loop for rows.
- Start a nested loop for columns.
- Read data from each cell using Worksheet.getCells().get(rowIndex, columnIndex).getValue() method.
The following code sample shows how to read data from a particular worksheet in Python.
Get a Free API License
You can use Aspose.Cells for Python via Java without evaluation limitations by requesting a free temporary license.
Conclusion
In this article, you have learned how to read Excel files in Python. Moreover, you have seen how to read data from a particular worksheet or all the worksheets in an Excel workbook. You can explore more about the Python spreadsheet library using the documentation. In case you would have any questions or queries, feel free to let us know via our forum.