跳過到頁腳內容
使用 IRONXL FOR PYTHON

如何在 Python 中讀取具有多個工作表的 Excel 文件

In Python, handling Microsoft Excel files is a common task, especially in data analysis, report generation, and automation workflows. Traditionally, libraries like openpyxl or pandas are used to manipulate Excel files. However, a lesser-known but powerful library called IronXL is gaining attention due to its robust capabilities and ease of use when working with Excel files in Python.

IronXL, a .NET-based library, enables reading and writing Excel files with various features such as handling multiple sheets, complex formatting, and supporting different Excel file formats. In this article, we’ll explore how to use IronXL Python to read Excel files with multiple sheets and demonstrate some of its key features and functionalities.

Introduction to IronXL

How to Read Excel File in Python With Multiple Sheets: Figure 1

IronXL for Python is a robust library designed to work with Microsoft Excel files. It allows developers to create, read, and manipulate Excel spreadsheets seamlessly. The main advantages of using IronXL include its ease of use, powerful functionalities, and the fact that it does not require Microsoft Excel to be installed on the server. Developers can read multiple files in an Excel file with ease.

How to Read Excel Files in Python with Multiple Sheets

Step 1: Import the IronXL Library.

Step 2: Add License Key.

Step 3: Load the Excel File.

Step 4: Accessing Sheets in the Workbook.

Why Use IronXL?

IronXL is ideal for various applications such as data analysis, reporting, and automation. Its intuitive API and comprehensive features make it a popular choice among developers. Some of the key features of IronXL include:

  • No Dependency on Microsoft Excel: IronXL does not require Microsoft Excel to be installed, making it suitable for server environments.
  • Support for Multiple Formats: It supports XLS files, XLSX files, and CSV file formats.
  • Cross-Platform Compatibility: IronXL works on Windows, macOS, Linux, Docker, Azure, and AWS.
  • Ease of Integration: With a natural and intuitive API, IronXL can be easily integrated into any Python project.

Step 1: Import the IronXL Library

Import IronXL using:

from ironxl import License, WorkBook, WorkSheet
from ironxl import License, WorkBook, WorkSheet
PYTHON

Step 2: Add License Key

IronXL works with a license key. Get your free license from here and place the license at the top of the code.

License.LicenseKey = "Your Key"
License.LicenseKey = "Your Key"
PYTHON

Step 3: Load the Excel File

You can load the Excel file using the WorkBook.Load() function. This method returns a Workbook object, representing the entire Excel file.

workbook = WorkBook.Load('sample.xlsx')
workbook = WorkBook.Load('sample.xlsx')
PYTHON

Step 4: Accessing Sheets in the Workbook

Once the workbook is loaded, you can access individual sheets. IronXL allows you to access sheets by name or index. Let’s assume your Excel file has multiple sheets.

To access all sheets in the workbook, you can iterate through them:

# Get sheet names from the workbook
sheet_names = workbook.WorkSheetsNames
print("Sheet Names:", sheet_names)

# Iterate through each sheet and read its contents
for sheet in workbook.WorkSheets:
    print(f"Sheet Name: {sheet.Name}")  # Access specific sheet
    for row in sheet.Rows:
        print([cell.Value for cell in row])  # Print each row's cell values
# Get sheet names from the workbook
sheet_names = workbook.WorkSheetsNames
print("Sheet Names:", sheet_names)

# Iterate through each sheet and read its contents
for sheet in workbook.WorkSheets:
    print(f"Sheet Name: {sheet.Name}")  # Access specific sheet
    for row in sheet.Rows:
        print([cell.Value for cell in row])  # Print each row's cell values
PYTHON

Advanced Features of IronXL

In addition to reading and writing data, IronXL comes with many advanced features that allow you to manipulate Excel files more effectively:

  • Cell Formatting: IronXL enables you to format cells with different styles, fonts, colors, and number formats.
  • Handling Different Excel Formats: It supports .xlsx, .xls, and .csv formats.
  • Performance: IronXL is optimized for high performance, making it suitable for working with large Excel files.
  • Write Data Back to Excel: Besides reading data, you can also modify and save changes to Excel files using IronXL.

Input Excel file

The Excel file has two sheets:

How to Read Excel File in Python With Multiple Sheets: Figure 2

How to Read Excel File in Python With Multiple Sheets: Figure 3

Full Code Example

To read all the sheets: Here's the complete code to read an Excel file with multiple sheets:

from ironxl import License, WorkBook, WorkSheet

# Set your IronXL license key
License.LicenseKey = "Your License Key"

# Load the Excel workbook
workbook = WorkBook.Load('sample.xlsx')

# Read Multiple Sheets
for sheet in workbook.WorkSheets:
    print(f"Sheet Name: {sheet.Name}")
    for row in sheet.Rows:
        print([cell.Value for cell in row])  # Print each row values
from ironxl import License, WorkBook, WorkSheet

# Set your IronXL license key
License.LicenseKey = "Your License Key"

# Load the Excel workbook
workbook = WorkBook.Load('sample.xlsx')

# Read Multiple Sheets
for sheet in workbook.WorkSheets:
    print(f"Sheet Name: {sheet.Name}")
    for row in sheet.Rows:
        print([cell.Value for cell in row])  # Print each row values
PYTHON

Code Explanation

  • Importing Libraries: It imports WorkBook and WorkSheet classes from the IronXL library, which are used to work with Excel files and sheets, respectively.
  • Loading the Excel Workbook: The Load() method is used to open the Excel file sample.xlsx and load it into a workbook object.
  • Iterating through Multiple Sheets: The code loops through all sheets in the workbook using the WorkSheets property. For each sheet, it prints the sheet's name.
  • Reading Rows in Each Sheet: For each sheet, it iterates through the rows and prints the values of each cell in that row as a list. The Value property of each cell is used to retrieve the content.

Output

How to Read Excel File in Python With Multiple Sheets: Figure 4

IronXL License (Trial Available)

IronXL works on a valid license file attached to the code. Users can easily get a trial license from the license page.

To use the license, place the license somewhere at the beginning in your code as shown below before using IronXL functionalities.

from ironxl import License

License.LicenseKey = "Your License Key"
from ironxl import License

License.LicenseKey = "Your License Key"
PYTHON

Conclusion

IronXL for Python is a powerful and efficient tool for handling Excel files with multiple sheets. Whether you're reading data, formatting cells, or handling larger files, IronXL simplifies the process, providing a clean and intuitive API. By following the steps above, you can quickly load and manipulate Excel files with multiple sheets in Python. IronXL stands out as an excellent choice for developers and data scientists who need to work with Excel in a seamless and effective way.

For more information and examples, you can refer to the IronXL Documentation.

常見問題解答

我如何在 Python 中讀取多個 Excel 工作表?

IronXL for Python 允許您通過使用 WorkBook.Load() 加載工作簿並迭代 WorkSheets 屬性來訪問和操作每個工作表,以讀取多個 Excel 工作表。

我需要安裝 Microsoft Excel 才能使用 IronXL 嗎?

不,IronXL 不需要安裝 Microsoft Excel,這使其非常適合於需要獨立處理 Excel 文件的服務器環境和應用程式。

IronXL 能夠處理什麼格式的 Excel 文件?

IronXL 支援多種格式,包括 XLS、XLSX 和 CSV,提供處理各種類型的 Excel 文件的靈活性,而無需外部軟體。

我如何將 IronXL 集成到我的 Python 項目?

您可以通過安裝庫、從 IronXL 許可證頁面獲取試用許可證密鑰,並在代碼中包含 License.LicenseKey = 'Your License Key' 來將 IronXL 集成到您的 Python 項目中。

使用 IronXL 比 openpyxl 或 pandas 有什麼好處?

IronXL 提供了強大的功能,例如不依賴於 Microsoft Excel、支援多種文件格式、跨平台兼容性和直觀的 API,使其非常適合於數據分析、報告和自動化。

IronXL 可以在不同的操作系統上使用嗎?

是的,IronXL 是跨平台兼容的,允許在 Windows、macOS、Linux、Docker、Azure 和 AWS 上使用,為各種開發環境提供靈活性。

我如何使用 IronXL 訪問 Excel 工作簿中的特定工作表?

在使用 WorkBook.Load() 加載工作簿後,可以通過名稱或索引通過 WorkSheets 屬性訪問特定的工作表。

使用 IronXL 加載 Excel 文件的過程是什麼?

要在 IronXL 中加載 Excel 文件,請使用 WorkBook.Load('filename.xlsx'),這將返回一個代表整個 Excel 文件的 WorkBook 物件,準備進行操作。

IronXL 如何幫助自動化與 Excel 有關的任務?

IronXL 提供了一個乾淨和直觀的 API,通過提供高級功能,例如單元格格式化和即使是大型文件也有高性能,來簡化諸如數據分析、報告生成和自動化等任務。

IronXL 的一些高級功能是什麼?

IronXL 包括高級功能,例如單元格格式化、支援不同的 Excel 格式、大文件的高性能以及將數據寫回 Excel 文件的能力,增強了其對複雜數據任務的實用性。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。