IronXL for Python 开始 IronXL Python 入门 Curtis Chau 已更新:六月 9, 2025 下载 IronXL pip 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronXL for Python 是由 Iron Software 开发的强大库,它为软件工程师提供了在 Python 3 项目中创建、读取和编辑 Excel(XLS、XLSX 和 CSV)文件的功能。 IronXL for Python 不需要在服务器上安装 Excel 或 Interop。IronXL for Python 提供的 API 比Microsoft.Office.Interop.Excel更快、更直观。 IronXL for Python 是在 IronXL for .NET 的成功和流行基础上建立起来的。 安装 IronXL for Python 前提条件 要使用 IronXL for Python,请确保计算机已安装以下必备软件: .NET 6.0 SDK :IronXL for Python 依赖于 IronXL .NET 库,特别是 .NET 6.0 作为其底层技术。 因此,要使用 IronXL for Python,必须在您的计算机上安装.NET 6.0 SDK 。 Python :从 Python 官方网站下载并安装最新版本的 Python 3.x:https://www.python.org/downloads/ 。 在安装过程中,确保选择将 Python 添加到系统 PATH 的选项,以便可以从命令行访问它。 Pip :从 Python 3.4 及更高版本开始,Pip 通常与 Python 安装程序捆绑在一起。 29. 然而,根据您的 Python 安装,您可能需要检查 pip 是否已安装或单独安装它。 IronXL 库:可以通过 pip 添加 IronXL。使用以下命令通过 pip 安装 IronXL: pip install IronXL 要安装特定版本的 IronXL,请使用以下语法: ==2023.xx 。 例如,您可以运行命令pip install ironxl==2023.xx .)}] 请注意在某些系统中,Python 2.x 可能仍是默认版本。 在这种情况下,您可能需要显式使用pip3命令而不是pip ,以确保您使用的是 Python 3 的 Pip。 读取 Excel 文档 使用 IronXL for Python 从 Excel 文件中读取数据只需要几行代码。 :path=/static-assets/excel-python/content-code-examples/get-started/get-started-1.py # Load the necessary module from IronXL from ironxl import WorkBook # Load an existing Excel spreadsheet # Replace 'sample.xlsx' with the path to your Excel file as needed. workbook = WorkBook.load("sample.xlsx") # Select the first worksheet from the workbook worksheet = workbook.worksheets[0] # Access cell A2 and get its integer value # Ensure the correct method or property is used to fetch the integer value. # Use 'value' to directly access the cell content. cell_value = worksheet["A2"].value # Print out the value of the cell A2 # Utilizing formatted strings for clear output print(f"Cell A2 has value '{cell_value}'") # Iterate over a range of cells and print their address and text content # The range is defined from A2 to B10, which captures all rows in this interval. for cell in worksheet.range("A2:B10"): # Access each cell in the specified range # AddressString is used to get the cell's location as a string, and Text to get its content. print(f"Cell {cell.address} has value '{cell.text}'") PYTHON 创建新的Excel文档 要使用 Python 创建 Excel 文档,IronXL for Python 提供了一个简单、快速的界面。 :path=/static-assets/excel-python/content-code-examples/get-started/get-started-2.py from ironxl import WorkBook, ExcelFileFormat, BorderType # Import necessary classes from ironxl # Create a new Excel WorkBook document in XLSX format workbook = WorkBook.create(ExcelFileFormat.XLSX) # Set metadata for the workbook workbook.metadata.author = "IronXL" # Add a new blank worksheet named "main_sheet" to the workbook worksheet = workbook.create_worksheet("main_sheet") # Add data to cell "A1" worksheet["A1"].value = "Hello World" # Set the style for cell "A2" with a double bottom border and a specific color worksheet["A2"].style.bottom_border.set_color("#ff6600") worksheet["A2"].style.bottom_border.type = BorderType.double # Save the Excel file with the specified filename workbook.save_as("NewExcelFile.xlsx") PYTHON 导出格式为 CSV、XLS、XLSX、JSON 或 XML 我们还可以保存或导出多种常见的结构化电子表格文件格式。 :path=/static-assets/excel-python/content-code-examples/get-started/get-started-3.py # Assuming workSheet is an existing instance of WorkSheet workSheet.SaveAs("NewExcelFile.xls") workSheet.SaveAs("NewExcelFile.xlsx") workSheet.SaveAsCsv("NewExcelFile.csv") workSheet.SaveAsJson("NewExcelFile.json") workSheet.SaveAsXml("NewExcelFile.xml") PYTHON 单元格和范围的样式 可以使用 Style 对象来设置 Excel 单元格和区域的样式。 :path=/static-assets/excel-python/content-code-examples/get-started/get-started-4.py # Set cell's value and styles workSheet["A1"].Value = "Hello World" workSheet["A2"].Style.BottomBorder.SetColor("#ff6600") workSheet["A2"].Style.BottomBorder.Type = BorderType.Double PYTHON 排序范围 使用 IronXL for Python,我们可以使用 Range 对一系列 Excel 单元格进行排序。 :path=/static-assets/excel-python/content-code-examples/get-started/get-started-5.py # Import IronXL library for handling Excel files from ironxl import WorkBook # Load an existing Excel workbook # 'sample.xls' is the file name of the Excel workbook to be loaded workbook = WorkBook.Load("sample.xls") # Access the first worksheet in the workbook # WorkSheets is the collection of all sheets in the workbook, # and we select the first one using index 0 worksheet = workbook.WorkSheets[0] # Select a range of cells from A2 to A8 in the worksheet # This specifies a contiguous range of cells starting from A2 and ending at A8 selected_range = worksheet["A2:A8"] # Sort the selected range of cells in ascending order # This operation reorders the values in the specified range from smallest to largest selected_range.SortAscending() # Save the changes made to the workbook, including the sorted range # The workbook's state is updated with the changes after execution workbook.Save() PYTHON 编辑公式 编辑 Excel 公式非常简单,只需在公式开头加上等号=即可赋值。 公式将实时计算。 :path=/static-assets/excel-python/content-code-examples/get-started/get-started-6.py # Set a formula workSheet["A1"].Formula = "=SUM(A2:A10)" # Get the calculated value sum_ = workSheet["A1"].DecimalValue PYTHON 为什么选择 IronXL 进行 Python 开发? IronXL for Python 为开发人员提供了简单易用的 API,方便他们读取和写入 Excel 文档。 IronXL for Python 不需要在服务器上安装 Microsoft Excel 或 Excel Interop 即可访问 Excel 文档。 这使得在 Python 中处理 Excel 文件成为一项非常快捷简单的任务。 可用的许可和支持 IronXL for Python可以在开发环境中免费使用和测试。 要在实际项目中使用,请购买许可证。 我们也提供30 天试用许可证。 如需查看完整的代码示例、教程、许可信息和文档,请访问: IronXL for Python 。 如需更多支持或有任何疑问,请联系我们的团队。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Version: 2025.9 刚刚发布 免费 pip 下载 查看许可证