USING IRONXL FOR PYTHON

How to read an Excel file in Python using Visual Studio Code

Published April 3, 2024
Share:

Introduction

Excel files are widely used to store and manipulate data. Some common tasks in Excel sheets usage include storing sales data and automating the calculation of sales forecast. However, manual manipulation can be laborious and prone to error when incorporating this data into your Python scripts. A common library that is often used with python when dealing with large data set is the usage of pandas library. However users needs to import pandas along with other dependences which would make it less than ideal in terms of scalability. Moreover the learning curve of panada is steep and its API is daunting for a beginner as such, this is where the robust Python module IronXL comes in, which makes working with Excel files easier.

This post teaches you how to read Excel files in Python using Visual Studio code. We will discuss advanced methods for effective data processing, go over the installation procedure, and examine key code examples for reading different data structures.

How to read an Excel file in Python using Visual Studio Code

  1. Create a new Project/ environment for Python using Visual Studio code.
  2. Install IronXL library for Python.
  3. Import the library into the Python code.
  4. Import the Excel file to be read.
  5. Select the worksheet and get the value using range or cell address.
  6. Process the value and display the result.

IronXL

IronXL is a robust Python package created especially to make working with Excel files (.xls,.xlsx, and .xlsm) in your Python projects easier. It provides an easy-to-use API for a range of operations, serving as a link between your Python code and the Excel spreadsheet universe.

Features of IronXL

  • Handling data: IronXL facilitates the reading, writing, and manipulating of data in Excel spreadsheets. Calculations, formulae, and data formatting are all possible, and cell values can be obtained using a two-dimensional array.
  • Creation and Modification of Excel Files: Developers can create new Excel files and edit existing ones, as well as add, remove, and manage worksheets. Additionally, they can use DLL files to interface with other Excel components.
  • . NET Integration and Cross-compatibility: IronXL can be integrated with various .NET platforms, such as Xamarin,.NET Core, and .NET Framework, and its cross-platform compatibility makes it suitable for use in a variety of application scenarios.
  • User-friendly API: The library is easy to use for developers of all skill levels thanks to its clear and well-documented API. To efficiently interact with your files, you don't need to be an expert in Excel structures.
  • No dependency: IronXL doesn't require Microsoft Office to be installed on the computer you're working on, in contrast to some other solutions. It operates autonomously, getting rid of compatibility problems and making deployment across many environments easier.
  • Rich Feature Set: IronXL is not limited to data reading; it provides an extensive range of functionalities. With Python code, you can use your Excel files to format cells, manage formulas, and generate charts. This gives you the ability to accomplish a variety of activities without having to physically alter the spreadsheet.
  • Data Extraction and Export: IronXL makes it easier to connect with databases and other systems by facilitating the extraction of data from Excel files and the export of Excel data to several formats, including XML, new data tables, and plain text.
  • Versatility and Compatibility: It works with several Excel versions and supports the more recent XLSX Excel format, and CSV file format in addition to the older XLS Excel format.

Data manipulation and Excel integration enable programmers to work with Excel files and provide dependable solutions. For more information on usage, please refer to this documentation.

Creating a New Project Folder

Launch the Visual Studio Code.

How to read an Excel file in Python using Visual Studio Code: Figure 1 - The Visual Studio Code blank project directory

Navigate to File > Open Folder (or press the keyboard shortcuts for Windows/Linux, Ctrl+K Ctrl+O, and macOS, Cmd+K Cmd+O).

How to read an Excel file in Python using Visual Studio Code: Figure 2 - Clicking on File then Select Folder  in Visual Studio Code to create a new project folder

Select a place on your PC where you wish to save your newly created project folder. To create the project folder, click "Select Folder".

Creating a Python File in VS Code

You can create a new Python file in the newly created project folder, which will contain your Python code.

There are two methods to do this:

  • Right-click anywhere in the project folder and choose New File. Name your Python file (say, my_script.py) after the .py extension.
  • Navigate to File > New File (or use Ctrl+N on Windows/Linux or Cmd+N on macOS to open a new file). Press Enter after naming your Python file with the .py extension.

Install IronXL

In Visual Studio Code, select Terminal > New Terminal to open a terminal window.

To install IronXL, use the pip package manager:

 pip install IronXL

How to read an Excel file in Python using Visual Studio Code: Figure 3 - Installing IronXL through pip package manager

Read Excel file Using IronXL

Reading Excel files can easily done by using IronXL with few lines of code. Let's start by creating the code necessary to obtain data stored from an Excel workbook and open it.

from ironxl import *
workbook = WorkBook.Load("Demo.xlsx")
worksheet = workbook.WorkSheets [0]
for cell in worksheet ["A2:A10"]:
    print("Cell {} has value '{}'".format(cell.AddressString, cell.Text))
PYTHON

Importing the IronXL library gives us access to all of its features. Then, we load the Microsoft Excel file that the user wishes to work with using ironxl.open_workbook function. The path to your Excel workbooks can be passed as a parameter in this example we are passing a .xlsx file. We can also pass CSV files as a parameter. Since Worksheets in Excel files can have more than one sheet, IronXL offers methods for gaining access to and utilizing multiple sheets.

Next, we take out multiple Excel sheets from the workbook using the worksheet name or access the Excel sheet by index if you don't know the names of the worksheets. Depending on your requirements, IronXL provides a variety of data access options. From the sheet, we can get the Excel range from A2 to A10. We can then use a for loop, to display the values one by one.

How to read an Excel file in Python using Visual Studio Code: Figure 4 - Console output from calling a loop to print out all cell text and associated values

The code above utilizes IronXL to read Excel files and displays all the data on a console as shown in the screenshot above.

To learn more about the IronXL code snippet and more related examples, please refer to the documentation.

Conclusion

Overall, IronXL is a strong and adaptable Python library for working with Excel files. Beyond reading and accessing data, it simplifies a variety of operations. IronXL enables developers to automate workflows and streamline Excel-related tasks within their Python applications. Key functionalities include creating and modifying spreadsheets, cell formatting, formula handling, and chart generation.

Its intuitive API, independence from Microsoft Office, and compatibility with other Excel file formats are among its main benefits. IronXL provides the required tools for automating report generation, cleaning and processing big datasets stored in Excel, and exporting Excel files to other formats.

IronXL offers a free options. They are more fully functional and offer more features and support. Visit the IronXL website for comprehensive and current information about licensing. We also offer a suite of other related software to increase a developer's productivity and streamline operation. Visit this website to learn more about Iron Software products.

< PREVIOUS
Python Modify Excel File (Developer Tutorial)
NEXT >
How to Export to Excel in Python

Ready to get started? Version: 2024.9 just released

Free pip Download View Licenses >