IronXL for Python 教程 Python 讀取 Excel 文件教程 How to Read Excel Files in Python with IronXL Curtis Chau 更新日期:6月 22, 2025 Download IronXL pip 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English This guide provides Python developers with step-by-step instructions on utilizing the IronXL library to read and edit Microsoft Excel documents. IronXL is a comprehensive Excel file processing library that supports multiple programming languages, including .NET and Python. This tutorial focuses specifically on using IronXL in Python scripts to read and edit Microsoft Excel documents. For a separate tutorial on how to read and edit Microsoft Excel documents in .NET applications, please refer to the following here. Reading and creating Excel files in Python is easy using the IronXL for Python software library. Overview How to Read Excel File in Python Download the Python Library to read Excel files Load and read an Excel file (workbook) Create an Excel workbook in CSV or XLSX Edit cell values in a range of cells Validate spreadsheet data Export data using Entity Framework Tutorial Step 1: Add IronXL as a Dependency in Your Python Project To integrate the IronXL library into your Python project, you must install it as a dependency using the widely used Python package manager, pip. Open the terminal and execute the following command: pip install IronXL This will install the specified version of IronXL in your project, making it accessible for import. 請注意IronXL for Python relies on the IronXL for .NET library. Therefore, it is necessary to have the .NET 6.0 SDK installed on your machine in order to use IronXL for Python. Step 2: Load an Excel Workbook The WorkBook class represents an Excel workbook. To open an Excel file, we use the WorkBook.Load method, specifying the path of the Excel file. :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-1.py # Load existing spreadsheet workbook = WorkBook.Load("Spreadsheets\\GDP.xlsx") PYTHON Each WorkBook can have multiple WorkSheet objects. Each one represents a single Excel worksheet in the Excel document. Use the WorkBook.get_worksheet method to retrieve a reference to a specific Excel worksheet. :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-2.py # Assuming workBook is an existing instance of WorkBook workSheet = workBook.GetWorkSheet("GDPByCountry") PYTHON Creating new Excel Documents To create a new Excel document, construct a new WorkBook object with a valid file type. :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-3.py # Create a new WorkBook with the specified Excel file format workBook = WorkBook(ExcelFileFormat.XLSX) PYTHON Note: Use ExcelFileFormat.XLS to support legacy versions of Microsoft Excel (95 and earlier). Add a Worksheet to an Excel Document As explained previously, an IronXL for Python WorkBook contains a collection of one or more WorkSheets. This is how one workbook with two worksheets looks in Excel. To create a new worksheet, call workbook.create_worksheet with the name of the worksheet. :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-4.py workSheet = workBook.CreateWorkSheet("GDPByCountry") PYTHON Access Cell Values Read and Edit a Single Cell Access to the values of individual spreadsheet cells is carried out by retrieving the desired cell from its WorkSheet as shown below: :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-5.py # Load existing spreadsheet workbook = WorkBook.Load("test.xlsx") worksheet = workbook.DefaultWorkSheet # Access cell B1 in the worksheet cell = worksheet["B1"] PYTHON IronXL for Python's Cell class represents an individual cell in an Excel spreadsheet. It contains properties and methods that enable users to access and modify the cell's value directly. With a reference to a Cell object, we can read and write data to and from a spreadsheet cell. Read and Write a Range of Cell Values The Range class represents a two-dimensional collection of Cell objects. This collection refers to a literal range of Excel cells. Obtain ranges by using the string indexer on a WorkSheet object. :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-6.py # Access cell B1 in the worksheet cell = workSheet["B1"] # Read the value of the cell as a string value = cell.StringValue print(value) # Write a new value to the cell cell.Value = "10.3289" print(cell.StringValue) PYTHON Add Formula to a Spreadsheet Set the formula of Cells with the formula property. :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-7.py # Access range D2:D101 in the worksheet range_ = workSheet["D2:D101"] PYTHON The code below iterates through each cell and sets a percentage total in column C. :path=/static-assets/excel-python/content-code-examples/tutorials/how-to-read-excel-file-csharp-8.py # Iterate through all rows with a value for y in range(2, i): # Get the C cell cell = workSheet[f"C{y}"] # Set the formula for the Percentage of Total column cell.Formula = f"=B{y}/B{i}" PYTHON Summary IronXL.Excel is a standalone Python library for reading a wide variety of spreadsheet formats. It does not require Microsoft Excel to be installed and is not dependent on Interop. 常見問題解答 我如何在 Python 中閱讀 Excel 文件? 您可以使用 IronXL 通過 WorkBook.Load 方法載入工作簿以在 Python 中閱讀 Excel 文件。只需提供 Excel 文件的路徑,例如 workbook = ironxl.WorkBook.load('path/to/workbook.xlsx')。 我可以在沒有安裝 Excel 的情況下在 Python 中編輯 Excel 文件嗎? 可以,使用 IronXL,您無需安裝 Microsoft Excel 就可以在 Python 中編輯 Excel 文件。您可以通過該庫直接修改儲存格值、添加工作表並應用公式。 如何在 Python 中安裝 IronXL 庫? 要在 Python 中安裝 IronXL,請使用 pip 包管理器在終端中運行命令 pip install ironxl。 在 Python 中使用 IronXL 進行 Excel 文件操作的優勢是什麼? IronXL 允許您在不需要 Microsoft Excel 的情況下操作 Excel 檔案。它提供了讀取、編輯、創建新工作簿和應用公式等功能,成為試算表操作的強大解決方案。 如何使用 IronXL 存取和修改 Excel 表格中的特定儲存格? 要使用 IronXL 存取 Excel 表格中的特定儲存格,請使用儲存格索引,如 cell = worksheet['A1']。您可以使用 cell.value 讀取值並通過 cell.value = 'New Value' 賦予新值來修改它。 在 IronXL 中有可能使用在線範圍嗎? 可以,IronXL 允許您使用 Range 類操作儲存格範圍。您可以使用類似字串索引器的方法存取一系列儲存格,例如 range_of_cells = worksheet['B2:E5'] 並進行操作。 如何在 Python 中使用 IronXL 把公式添加到 Excel 儲存格? 要使用 IronXL 向 Excel 儲存格添加公式,設置儲存格的 formula 屬性。例如,cell.formula = '=A1+B1' 會將儲存格 A1 和 B1 的值相加。 IronXL 是否需要 Microsoft Office Interop? 不,IronXL 不需要 Microsoft Office Interop。它獨立於 Microsoft Excel 運行,使您在無需額外的依賴項下處理 Excel 檔案。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? 版本: 2025.9 剛剛發布 免費 pip 下載 查看許可證