跳至页脚内容
使用 IRONXL FOR PYTHON

如何在 Python 中读取 Excel 文件

Working with Excel files is a common requirement in data analysis and automation tasks. Python, with its extensive set of libraries, offers various ways to interact with XLSX files. In this tutorial, we'll learn how to read Excel files using IronXL, a Python Excel library.

IronXL: Python Excel Library

IronXL for Python is designed to help developers read and write Excel files easily in Python. Not only can you write Excel files, but you can also work with multiple Excel sheets in a single go. This library is perfect for writing Excel files without installing Microsoft Excel on your machine.

When you need to import data directly into an Excel spreadsheet, IronXL comes to the rescue. IronXL makes handling Excel spreadsheets simple. It helps to manage data across multiple sheets in an XLSX file easily.

Let's see how we can read Excel files in Python using IronXL.

How to Read Excel Files in Python

The following steps go over the process involved in reading Excel files in Python.

  1. Create a Python file in Visual Studio code.
  2. Install the Python Excel library using pip.
  3. Load the Excel file which we need to read
  4. Iterate Excel data using loops
  5. Show iterated data on the console

Prerequisites

Before diving into the tutorial, ensure you have the following prerequisites installed on your system:

  • .NET 6.0 SDK: IronXL is built on .NET, requiring the .NET 6.0 SDK to be installed on your system.
  • Python 3.0+: The tutorial assumes you have Python 3.0 or higher installed.
  • pip: Ensure pip, Python's package installer, is installed as it will be used to install IronXL.

Setting Up Your Environment

1. Creating a File in Visual Studio Code

Open Visual Studio Code and create a new Python file named ReadExcel.py. This file will contain our script to read Excel files using IronXL.

How to Read Excel Files in Python: Figure 1

2. Installing IronXL

Open the command line within Visual Studio Code by selecting Terminal > New Terminal from the menu.

How to Read Excel Files in Python: Figure 2

Install IronXL by running the following command:

pip install ironxl
pip install ironxl
SHELL

How to Read Excel Files in Python: Figure 3

Writing the Code

Now, let's break down the code into sections and explain each part.

Importing Libraries

from ironxl import *
import sys
from ironxl import *
import sys
PYTHON

Here, we import everything from the ironxl library, which provides the functionality to work with Excel files. sys is used to set the path where Python is installed, which is necessary for IronXL to function correctly.

Setting Python Installation Path

sys.prefix = r'C:\Users\User Name\AppData\Local\Programs\Python\Python312'
sys.prefix = r'C:\Users\User Name\AppData\Local\Programs\Python\Python312'
PYTHON

sys.prefix is assigned the path to your Python installation directory. This preparatory step might not be necessary in every environment but is essential in some configurations to prevent path-related issues.

Setting License Key

License.LicenseKey = "License-Key"
License.LicenseKey = "License-Key"
PYTHON

The IronXL library requires a valid license key to unlock all its features. This line of code License.LicenseKey = "License-Key" is where you would insert your obtained license key. Without a valid license, IronXL will not work.

Loading the Workbook

workbook = WorkBook.Load("data.xlsx")
workbook = WorkBook.Load("data.xlsx")
PYTHON

This section of the script, workbook = WorkBook.Load("data.xlsx"), demonstrates how to load an Excel workbook. WorkBook.Load is a method provided by IronXL to open an existing Excel file, specified by its file path.

The example uses "data.xlsx", assuming this file is located in the same directory as the script. If your file is elsewhere, you'll need to provide the complete file path.

Selecting a Worksheet

worksheet = workbook.WorkSheets[0]
worksheet = workbook.WorkSheets[0]
PYTHON

After loading the workbook, the next step is to select a worksheet. The workbook.WorkSheets[0] line accesses the first worksheet in the Excel file. Worksheets are indexed starting at 0, so this code snippet effectively selects the first sheet.

Reading and Displaying Data

print("Sample data from A2:A5:")
for cell in worksheet["A2:A5"]:
    print("Cell {} has value '{}'".format(cell.AddressString, cell.Text))
print("Sample data from A2:A5:")
for cell in worksheet["A2:A5"]:
    print("Cell {} has value '{}'".format(cell.AddressString, cell.Text))
PYTHON

This part of the script exemplifies how to read and display data from specific cells. By iterating over a cell range for the cells in the worksheet["A2:A5"], we access each cell's address and text value. You can retrieve information from predefined areas within a sheet.

Performing Calculations

IronXL provides functions to perform simple calculations directly on data ranges within an Excel sheet. The following sections of the code demonstrate how to perform simple calculations like sum, minimum, and maximum on data within a specified range.

total_sum = worksheet["B2:B10"].Sum()
print("Total Sum of B2:B10: ", total_sum)

minimum_value = worksheet["C2:C10"].Min()
print("Minimum Value in C2:C10: ", minimum_value)

maximum_value = worksheet["D2:D10"].Max()
print("Maximum Value in D2:D10: ", maximum_value)
total_sum = worksheet["B2:B10"].Sum()
print("Total Sum of B2:B10: ", total_sum)

minimum_value = worksheet["C2:C10"].Min()
print("Minimum Value in C2:C10: ", minimum_value)

maximum_value = worksheet["D2:D10"].Max()
print("Maximum Value in D2:D10: ", maximum_value)
PYTHON

Finding Unique Values

This part of the script demonstrates how to identify unique values within a specified range of an Excel sheet:

unique_values = set(cell.Text for cell in worksheet["F2:F10"])
print("Unique Values in F2:F10: ", unique_values)
unique_values = set(cell.Text for cell in worksheet["F2:F10"])
print("Unique Values in F2:F10: ", unique_values)
PYTHON

To achieve this, a set comprehension is utilized. Sets in Python are collections that automatically eliminate duplicate entries, making them perfect for finding unique values. The comprehension iterates over each cell in the range F2:F10, accessing the text content of each cell using cell.Text.

By placing this iteration inside a set, we ensure that only unique values are kept.

Counting Occurrences of a Specific Value

Finally, this snippet counts how many times a specific value occurs in a given range.

specific_value = '5'  # Adjust this as needed based on your data
occurrences = sum(cell.Text == specific_value for cell in worksheet["G2:G10"])
print(f"Occurrences of value '{specific_value}' in G2:G10: ", occurrences)
specific_value = '5'  # Adjust this as needed based on your data
occurrences = sum(cell.Text == specific_value for cell in worksheet["G2:G10"])
print(f"Occurrences of value '{specific_value}' in G2:G10: ", occurrences)
PYTHON

The goal here is to quantify the occurrence of a specific value, labelled here as specific_value. The code employs a generator expression that iterates over each cell in the range G2:G10, comparing the cell's text content to specific_value.

The expression cell.Text == specific_value evaluates to True for each cell containing the target value and False otherwise. The sum function adds up these Boolean values, treating True as 1 and False as 0, effectively counting the number of times the specific value occurs.

Full Code Example

Here is the complete code for a better understanding of the code:

from ironxl import *
import sys

# Setting Python installation path
sys.prefix = r'C:\Users\User Name\AppData\Local\Programs\Python\Python312'

# Setting the license key for IronXL
License.LicenseKey = "License-Key"

# Load the workbook
workbook = WorkBook.Load("data.xlsx")

# Select the worksheet at index 0
worksheet = workbook.WorkSheets[0]

# Display values in a specific range as an example
print("Sample data from A2:A5:")
for cell in worksheet["A2:A5"]:
    print("Cell {} has value '{}'".format(cell.AddressString, cell.Text))

# Calculate the sum of values in a different range
total_sum = worksheet["B2:B10"].Sum()
print("Total Sum of B2:B10: ", total_sum)

# Calculate the minimum value in another range
minimum_value = worksheet["C2:C10"].Min()
print("Minimum Value in C2:C10: ", minimum_value)

# Calculate the maximum value in a different range
maximum_value = worksheet["D2:D10"].Max()
print("Maximum Value in D2:D10: ", maximum_value)

# Find unique values in a specified range
unique_values = set(cell.Text for cell in worksheet["F2:F10"])
print("Unique Values in F2:F10: ", unique_values)

# Count occurrences of a specific value in a different range
specific_value = '5'  # Adjust this as needed based on your data
occurrences = sum(cell.Text == specific_value for cell in worksheet["G2:G10"])
print(f"Occurrences of value '{specific_value}' in G2:G10: ", occurrences)
from ironxl import *
import sys

# Setting Python installation path
sys.prefix = r'C:\Users\User Name\AppData\Local\Programs\Python\Python312'

# Setting the license key for IronXL
License.LicenseKey = "License-Key"

# Load the workbook
workbook = WorkBook.Load("data.xlsx")

# Select the worksheet at index 0
worksheet = workbook.WorkSheets[0]

# Display values in a specific range as an example
print("Sample data from A2:A5:")
for cell in worksheet["A2:A5"]:
    print("Cell {} has value '{}'".format(cell.AddressString, cell.Text))

# Calculate the sum of values in a different range
total_sum = worksheet["B2:B10"].Sum()
print("Total Sum of B2:B10: ", total_sum)

# Calculate the minimum value in another range
minimum_value = worksheet["C2:C10"].Min()
print("Minimum Value in C2:C10: ", minimum_value)

# Calculate the maximum value in a different range
maximum_value = worksheet["D2:D10"].Max()
print("Maximum Value in D2:D10: ", maximum_value)

# Find unique values in a specified range
unique_values = set(cell.Text for cell in worksheet["F2:F10"])
print("Unique Values in F2:F10: ", unique_values)

# Count occurrences of a specific value in a different range
specific_value = '5'  # Adjust this as needed based on your data
occurrences = sum(cell.Text == specific_value for cell in worksheet["G2:G10"])
print(f"Occurrences of value '{specific_value}' in G2:G10: ", occurrences)
PYTHON

Output

After running the ReadExcel.py Python file, you'll see the following output displayed in your console, which reflects the results of the operations carried out by the script on the Excel data.

How to Read Excel Files in Python: Figure 4

Conclusion

In this tutorial, we've explored how to set up a Python environment to use IronXL for reading Excel files. We've covered installing the necessary software, writing the script to load an Excel workbook, select a worksheet, read the data, perform basic data analysis operations, and more.

IronXL provides a powerful API for working with Excel files in Python, enabling data analysis, data cleaning, and visualization tasks without needing Microsoft Excel.

Whether you're analyzing sales data, importing data for reports, or creating visualizations from Excel data, IronXL offers a robust solution for managing Excel files in Python applications.

IronXL provides a free trial for users to explore its features, with licenses beginning at $799 for those ready to fully integrate its capabilities.

常见问题解答

如何在没有 Microsoft Excel 的情况下在 Python 中读取 Excel 文件?

IronXL 允许您在没有系统中安装 Microsoft Excel 的情况下在 Python 中读取 Excel 文件。您可以使用 WorkBook.Load("data.xlsx") 加载工作簿,并直接访问工作表和单元格数据。

使用 IronXL 在 Python 中读取 Excel 文件需要什么设置?

要使用 IronXL,请确保您已安装 .NET 6.0 SDK、Python 3.0 或更高版本以及 pip。在 Visual Studio Code 中设置您的编程环境以开始处理 Excel 文件。

如何在 Python 中安装 IronXL?

在终端中运行命令 pip install ironxl 安装 IronXL。这样即可安装库,允许您读取和操作 Excel 文件。

我可以使用 IronXL 对 Excel 文件进行数据分析操作吗?

是的,IronXL 支持各种数据分析操作,例如计算总和、查找最小和最大值以及识别 Excel 表格中的唯一值。

如何使用 IronXL 在 Excel 范围内查找唯一值?

要查找唯一值,可以使用集合推导来遍历单元格范围并提取各个文本值。这种方法可以有效识别所有唯一条目。

是否可以使用 IronXL 计算 Excel 中的特定值?

是的,您可以通过使用生成器表达式遍历单元格范围,并总结与目标值相匹配的单元格文本实例来计算特定值的出现次数。

使用 IronXL 进行 Excel 操作的好处是什么?

IronXL 提供了一个强大的 API 用于 Excel 文件操作,而不需要 Microsoft Excel。它对于数据分析、清理、可视化等任务非常理想,从而提高 Python 项目的生产力。

如何编写 Python 脚本以使用 IronXL 读取 Excel 文件?

创建一个名为 ReadExcel.py 的 Python 脚本,导入必要的库,设置您的 Python 路径,然后使用 IronXL 的方法加载并与 Excel 工作簿和数据交互。

我可以在 Excel 数据上执行哪些操作使用 IronXL?

IronXL 允许各种操作,例如读取单元格值、选择工作表、计算总和、查找最小/最大值,以及在 Excel 数据范围内计算特定出现情况。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。