IronXL for Python 开始 Introduction to IronXL for Python Curtis Chau 已更新:六月 9, 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 IronXL for Python is a powerful library developed by Iron Software, offering software engineers the capability to create, read, and edit Excel (XLS, XLSX, and CSV) files in Python 3 projects. IronXL for Python does not require Excel to be installed on your server or Interop. IronXL for Python provides a faster and more intuitive API than Microsoft.Office.Interop.Excel. IronXL for Python builds upon the success and popularity of IronXL for .NET. Install IronXL for Python Prerequisites To use IronXL for Python, please ensure that the computer has the following prerequisite software installed: .NET 6.0 SDK: IronXL for Python relies on IronXL .NET library, specifically .NET 6.0, as its underlying technology. Therefore, it is necessary to have the .NET 6.0 SDK installed on your machine in order to use IronXL for Python. Python: Download and install the latest version of Python 3.x from the official Python website: https://www.python.org/downloads/. During the installation process, make sure to select the option to add Python to the system PATH, which will make it accessible from the command line. Pip: Pip is usually bundled with Python installation starting from Python 3.4 and later. However, depending on your Python installation, you may need to check if pip is already installed or install it separately. IronXL Library: The IronXL library can be added via pip. Use the command below to install IronXL using pip: pip install IronXL 提示To install a specific version of IronXL, please use the following syntax: ==2023.x.x. For example, you can run the command pip install ironxl==2023.x.x. 请注意On some systems, Python 2.x may still be the default version. In such cases, you may need to explicitly use the pip3 command instead of pip to ensure that you're using Pip for Python 3. Reading an Excel Document Reading data from an Excel file with IronXL for Python takes a few lines of code. :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 Creating New Excel Documents To create Excel documents in Python, IronXL for Python provides a simple, fast interface. :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 Exporting as CSV, XLS, XLSX, JSON or XML We can also save or export as many common structured spreadsheet file formats. :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 Styling Cells and Ranges Excel cells and ranges can be styled using the Style object. :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 Sorting Ranges Using IronXL for Python we can sort a range of Excel Cells using Range. :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 Editing Formulas Editing an Excel formula is as easy as assigning a value with an = equals sign at the start. The formula will be calculated live. :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 Why Choose IronXL for Python? IronXL for Python features an easy API for developers to read and write Excel documents. IronXL for Python does not require installation of Microsoft Excel on your server or Excel Interop to access Excel documents. This makes working with Excel files in Python, a very quick and simple task. Licensing & Support Available IronXL for Python is free to use and test in development environments. To use in live projects purchase a license. 30 day Trial licenses are also available. For our full list of code examples, tutorials, licensing information, and documentation visit: IronXL for Python. For more support and inquiries, please ask our team. Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? 版本: 2025.9 刚刚发布 免费 pip 下载 查看许可证