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

如何在 Visual Studio Code 中使用 Python 讀取 Excel 文件

Excel files are widely used to store and manipulate data. Common tasks include storing sales data and automating the calculation of sales forecasts. However, manual manipulation can be laborious and prone to errors when incorporating this data into your Python scripts. A common library used in Python for dealing with large datasets is pandas. However, users need to import pandas along with other dependencies, which may not be ideal for scalability. Additionally, the learning curve for pandas can be steep, and its API daunting for beginners. This is where the robust Python module IronXL comes in, making 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 the 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 a 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 Excel spreadsheets.

Features of IronXL

  • Handling data: IronXL facilitates the reading, writing, and manipulation of data in Excel spreadsheets. It supports calculations, formulae, and data formatting, 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.
  • .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. It operates autonomously, eliminating compatibility problems and simplifying deployment across many environments.
  • Rich Feature Set: IronXL provides a range of functionalities beyond data reading, including cell formatting, formula handling, and chart generation. This enables various activities without directly altering the spreadsheet.
  • Data Extraction and Export: IronXL simplifies connecting with databases and other systems by facilitating data extraction from Excel files and exporting Excel data to multiple formats, including XML, new data tables, and plain text.
  • Versatility and Compatibility: It supports several Excel versions and formats, including XLSX, CSV, and older XLS formats.

For more information on usage, please refer to this documentation.

Creating a New Project Folder

Launch Visual Studio Code.

Visual Studio Code project directory

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

Select Folder in Visual Studio Code

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

Creating a Python File in VS Code

Create a new Python file in the project folder to contain your Python code.

Two methods to do this:

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

Install IronXL

In Visual Studio Code, open a terminal window by selecting Terminal > New Terminal.

To install IronXL, use the following pip command in your terminal:

pip install ironxl
pip install ironxl
SHELL

Installing IronXL through pip

Read Excel file Using IronXL

Reading Excel files is easily done using IronXL with a few lines of code.

from ironxl import WorkBook

# Load an existing Excel workbook
workbook = WorkBook.Load("Demo.xlsx")

# Access the first worksheet
worksheet = workbook.WorkSheets[0]

# Iterate over a range of cells and print their values
for cell in worksheet["A2:A10"]:
    print(f"Cell {cell.AddressString} has value '{cell.Text}'")
from ironxl import WorkBook

# Load an existing Excel workbook
workbook = WorkBook.Load("Demo.xlsx")

# Access the first worksheet
worksheet = workbook.WorkSheets[0]

# Iterate over a range of cells and print their values
for cell in worksheet["A2:A10"]:
    print(f"Cell {cell.AddressString} has value '{cell.Text}'")
PYTHON

Explanation:

  1. Import Library: Importing the IronXL library gives access to its features.
  2. Load Workbook: Load the Excel workbook using WorkBook.Load("Demo.xlsx"). The path to the workbook is specified here.
  3. Access Worksheet: Access worksheets by index (e.g., WorkSheets[0] for the first worksheet).
  4. Iterate Cells: Use a for loop to iterate through a specified cell range (e.g., A2:A10), printing out each cell's address and value.

Console output showing cell values

The code above demonstrates reading Excel files with IronXL and outputs the data to a console.

For more related examples and documentation, please refer to the IronXL documentation.

Conclusion

Overall, IronXL is a powerful and versatile Python library for working with Excel files. Beyond reading and accessing data, it simplifies a variety of operations, enabling developers to automate workflows and streamline Excel-related tasks within 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 necessary tools for automating report generation, cleaning and processing large datasets stored in Excel, and exporting Excel files to other formats.

IronXL provides a free licensing option. Visit the IronXL website for comprehensive and current licensing information. Additional related software is available to enhance developer productivity. Visit the Iron Software website to learn more.

常見問題解答

如何在 Python 中使用 Visual Studio Code 讀取 Excel 文件?

您可以通過安裝 IronXL 在 Python 中使用 Visual Studio Code 讀取 Excel 文件。 首先,設置一個 Python 項目並使用命令pip install ironxl安裝 IronXL。 然後,在您的 Python 腳本中導入 IronXL 庫,使用WorkBook.Load()加載工作簿,訪問工作表,並遍歷單元格以提取數據。

在 Python 中使用 IronXL 進行 Excel 操作有哪些優勢?

IronXL 相較於 pandas 提供了多個優勢,包括更友好的 API,不需要額外的依賴性要求,以及更簡單的可擴展性。 它尤其對於初學者有益,因為其直觀的設計和提供了強大的 Excel 文件操作功能,而不需要 Microsoft Office。

如何安裝 IronXL 來操控 Python 中的 Excel 文件?

要安裝 Python 中的 IronXL 以操控 Excel 文件,請在 Visual Studio Code 中打開終端或命令提示。使用命令pip install ironxl即可。 這將下載並安裝庫,使其可用於您的 Python 腳本中。

IronXL 是否能在未安裝 Microsoft Office 的情況下處理 Excel 文件?

是的,IronXL 能夠在不需要安裝 Microsoft Office 的情況下處理 Excel 文件。 此功能便於在不同環境中部署,使其成為 Python 中 Excel 文件操作的多功能工具。

IronXL 支持哪些 Excel 文件格式?

IronXL 支持多種 Excel 文件格式,包括 XLSX、CSV 和舊的 XLS 格式。 這為在 Python 中進行 Excel 文件操作提供了靈活性和兼容性。

IronXL 如何簡化從 Excel 文件中提取數據的過程?

IronXL 通過允許用戶輕鬆加載 Excel 文件、訪問工作表和遍歷單元格來提取和處理數據,從而簡化了數據提取過程。 它還支持將數據導出為多種格式,例如 XML 和純文本,從而促進與其他系統的集成。

IronXL 是否提供免費的許可選項?

是的,IronXL 為用戶提供免費的許可選項。 關於許可的更多信息,您可以訪問 IronXL 網站,該網站提供了有關價格和許可選擇的詳細信息。

在哪裡可以找到關於在 Python 中使用 IronXL 與 Excel 的更多資源和示例?

在 IronXL 的官方網站上的文檔頁面上可以找到有關在 Python 中使用 IronXL 與 Excel 的更多資源、示例和文檔。 這包括指南、教程和 API 參考,以幫助您開始使用。

Curtis Chau
技術作家

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

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