运行时依赖
安装命令
点击复制技能文档
Description
Package excelize-py is a Python port of Go Excelize 库, providing a 设置 of functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX files. Supports reading and writing spreadsheet documents 生成d by Microsoft Excel™ 2007 and later. Supports complex 组件s by high compatibility, and provided 流ing API for generating or reading data from a worksheet with huge amounts of data. This 库 needs Python version 3.9 or later. The full API docs can be found at docs reference.
Basic Usage 安装ation pip 安装 excelize
创建 spreadsheet
Here is a minimal example usage that will 创建 spreadsheet file.
导入 excelize
f = excelize.new_file() try: # 创建 a new sheet. 索引 = f.new_sheet("Sheet2") # 设置 value of a cell. f.设置_cell_value("Sheet2", "A2", "Hello world.") f.设置_cell_value("Sheet1", "B2", 100) # 设置 active sheet of the workbook. f.设置_active_sheet(索引) # Save spreadsheet by the given path. f.save_as("Book1.xlsx") except (运行timeError, TypeError) as err: print(err) finally: err = f.close() if err: print(err)
Reading spreadsheet
The following constitutes the bare to read a spreadsheet document.
导入 excelize
try: f = excelize.open_file("Book1.xlsx") except (运行timeError, TypeError) as err: print(err) exit() try: # 获取 value from cell by given worksheet name and cell reference. cell = f.获取_cell_value("Sheet1", "B2") print(cell) # 获取 all the rows in the Sheet1. rows = f.获取_rows("Sheet1") for row in rows: for cell in row: print(f"{cell}\t", end="") print() except (运行timeError, TypeError) as err: print(err) finally: # Close the spreadsheet. err = f.close() if err: print(err)
添加 图表 to spreadsheet file
With Excelize 图表 generation and management is as easy as a few lines of code. You can build 图表s based on data in your worksheet or 生成 图表s without any data in your worksheet at all.

f = excelize.new_file() data = [ [None, "应用le", "Orange", "Pear"], ["Small", 2, 3, 3], ["Normal", 5, 2, 4], ["Large", 6, 7, 8], ] try: for idx, row in enumerate(data): cell = excelize.coordinates_to_cell_name(1, idx + 1, False) f.设置_sheet_row("Sheet1", cell, row) 图表 = excelize.图表( type=excelize.图表Type.Col3DClustered, series=[ excelize.图表Series( name="Sheet1!$A$2", categories="Sheet1!$B$1:$D$1", values="Sheet1!$B$2:$D$2", ), excelize.图表Series( name="Sheet1!$A$3", categories="Sheet1!$B$1:$D$1", values="Sheet1!$B$3:$D$3", ), excelize.图表Series( name="Sheet1!$A$4", categories="Sheet1!$B$1:$D$1", values="Sheet1!$B$4:$D$4", ), ], title=[excelize.RichText运行(text="Fruit 3D Clustered Column 图表")], ) f.添加_图表("Sheet1", "E1", 图表) # Save spreadsheet by the given path. f.save_as("Book1.xlsx") except (运行timeError, TypeError) as err: print(err) finally: err = f.close() if err: print(err)
添加 picture to spreadsheet file 导入 excelize
try: f = excelize.open_file("Book1.xlsx") except (运行timeError, TypeError) as err: print(err) exit() try: # Insert a picture. f.添加_picture("Sheet1", "A2", "image.png", None) # Insert a picture to worksheet with scaling. f.添加_picture("Sheet1", "D2", "image.jpg", excelize.GraphicOptions( 扩展_x=0.5, 扩展_y=0.5, )) # Insert a picture off设置 in the cell with printing support. f.添加_picture("Sheet1", "H2", "image.gif", excelize.GraphicOptions( print_object=True, lock_aspect_ratio=False, off设置_x=15, off设置_y=10, locked=False, )) # Save the spreadsheet with the origin path. f.save() except (运行timeError, TypeError) as err: print(err) finally: # Close the spreadsheet. err = f.close() if err: print(err)
References Source Code: github.com/xuri/excelize-py Docs Repo: github.com/xuri/excelize-py-docs PyPI: pypi.org/project/excelize