网站首页 > 基础教程 正文
Excel 中的表格是一个强大的工具,可以帮助我们有效地管理、组织和分析数据。通过将数据范围转换为表格,我们可以轻松地对数据进行筛选、排序和计算
用于在 Excel 中处理表格的 Python 库
Spire.XLS for Python 提供了用于处理 Excel 文件的广泛功能。我们可以使用它来创建、修改和删除 Excel 文档中的表格、图表和许多其他对象。此外,Spire.XLS 支持多种 Excel 格式,例如 .xls、.xlsx、.xlsm、.xlsb 等,使其成为 Python 中数据处理任务的多功能工具。
安装
我们可以通过运行以下命令从 PyPI 安装 Spire.XLS for Python:
pip install spire.xls
安装后,我们可以开始处理 Excel 文件中的表格。
使用 Python 在 Excel 中创建表格
Worksheet.ListObjects.Create() 方法用于将 Excel 工作表中的指定数据区域转换为表格。添加表格后,我们可以通过 BuiltInTableStyle 属性应用内置样式来进一步增强其外观和可读性。
以下是如何在 Excel 中创建表格并使用 Python 对其应用样式的代码示例:
from spire.xls import *
from spire.xls.common import *
# Open an Excel file
workbook = Workbook()
workbook.LoadFromFile("Template.xlsx")
# Get the desired worksheet by index (zero-based)
worksheet = workbook.Worksheets[0]
# Specify the data range that you want to convert to a table
range = worksheet.Range["A1:G11"]
# Convert the data range to a table
table = worksheet.ListObjects.Create("Table1", range)
# Set a built-in style for the table
table.BuiltInTableStyle = TableBuiltInStyles.TableStyleMedium2
# Save the resulting workbook to a file
workbook.SaveToFile("CreateTable.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 在 Excel 中创建表格
使用 Python 更改 Excel 表格的名称和数据范围
Excel 工作表中的现有表可以通过 Worksheet.ListObjects[index] 属性进行访问。访问后,我们可以使用 DisplayName 和 Location 属性更改其名称和数据范围。
以下是如何使用 Python 在 Excel 中更改现有表的名称和数据范围的代码示例:
from spire.xls import *
from spire.xls.common import *
# Open an Excel file
workbook = Workbook()
workbook.LoadFromFile("CreateTable.xlsx")
# Get the desired worksheet by index (zero-based)
worksheet = workbook.Worksheets[0]
# Get the desired table by index (zero-based)
table = worksheet.ListObjects[0]
# Specify the new data range of the table
new_range = worksheet.Range["C1:G11"]
# Change the data range of the table
table.Location = new_range
# Change the name of the table
table.DisplayName = "Product_Sales"
# Save the resulting workbook to a file
workbook.SaveToFile("ChangeTableNameAndDataRange.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 向 Excel 表格添加总计行
我们可以通过将 DisplayTotalRow 属性设置为 True 在表的底部添加总计行。还可以设置用于总计算的函数,例如 sum、average、min 或 max。
以下是如何使用 Python 在 Excel 中现有表的底部添加总计行的代码示例:
from spire.xls import *
from spire.xls.common import *
# Open an Excel file
workbook = Workbook()
workbook.LoadFromFile("CreateTable.xlsx")
# Get the desired worksheet by index (zero-based)
worksheet = workbook.Worksheets[0]
# Get the desired table by index (zero-based)
table = worksheet.ListObjects[0]
# Display total row
table.DisplayTotalRow = True
# Add total row label
table.Columns[0].TotalsRowLabel = "Total"
# Set the function used for the total calculation
table.Columns[5].TotalsCalculation = ExcelTotalsCalculation.Sum
table.Columns[6].TotalsCalculation = ExcelTotalsCalculation.Sum
# Save the resulting workbook to a file
workbook.SaveToFile("AddTotalRow.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 在 Excel 表中插入或删除行或列
有时,我们可能希望通过添加或删除行和列来修改表的结构。这可以通过在工作表中的适当位置插入行和列来轻松实现。
以下是如何使用 Python 在 Excel 中的现有表中插入和删除行和列的代码示例:
from spire.xls import *
from spire.xls.common import *
# Open an Excel file
workbook = Workbook()
workbook.LoadFromFile("CreateTable.xlsx")
# Get the desired worksheet by index (zero-based)
worksheet = workbook.Worksheets[0]
# Insert a row and column at a specific position
worksheet.InsertRow(5, 1, InsertOptionsType.FormatAsBefore)
worksheet.InsertColumn(3, 1, InsertOptionsType.FormatAsBefore)
# Delete a row and column
worksheet.DeleteRow(6, 1)
worksheet.DeleteColumn(4, 1)
# Save the resulting workbook to a file
workbook.SaveToFile("InsertOrDeleteRowsAndColumnsFromTable.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 自定义 Excel 表格的表格样式选项
我们可以自定义表格样式选项,例如标题行、总计行、第一列、最后一列、带状行和带状列,以使表格更易于阅读。
以下是如何使用 Python 在 Excel 中自定义表格样式选项的代码示例:
from spire.xls import *
from spire.xls.common import *
# Open an Excel file
workbook = Workbook()
workbook.LoadFromFile("CreateTable.xlsx")
# Get the desired worksheet by index (zero-based)
worksheet = workbook.Worksheets[0]
# Get the desired table by index (zero-based)
table = worksheet.ListObjects[0]
# Display total row
table.DisplayTotalRow = False
# Display header row
table.DisplayHeaderRow = True
# Display special formatting for the first column
table.DisplayFirstColumn= True
# Display special formatting for the last column
table.DisplayLastColumn= True
# Display banded rows
table.ShowTableStyleRowStripes = True
# Display banded columns
table.ShowTableStyleColumnStripes = True
# Save the resulting workbook to a file
workbook.SaveToFile("CustomizeTableStyleOptions.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
使用 Python 删除 Excel 中的表格
删除表格意味着将其转换回正常的单元格范围。此过程将删除表结构,同时保持数据完整。我们可以通过使用表的索引或名称从 Excel 工作表中删除表,以及从 Excel 工作表中删除所有表。
以下是如何使用 Python 将 Excel 工作表中的表格或所有表格转换为正常单元格范围的代码示例:
from spire.xls import *
from spire.xls.common import *
# Open an Excel file
workbook = Workbook()
workbook.LoadFromFile("CreateTable.xlsx")
# Get the desired worksheet by index (zero-based)
worksheet = workbook.Worksheets[0]
# Remove the desired table by index (zero-based)
worksheet.ListObjects.RemoveAt(0)
# # Or remove the desired table by name
# for i in range(len(worksheet.ListObjects) - 1, -1, -1):
# # Check if the table name matches the specific value
# if worksheet.ListObjects[i].Name == "Table1":
# # Remove the table
# worksheet.ListObjects.RemoveAt(i)
# # Or remove all tables from the worksheet
# worksheet.ListObjects.Clear()
# Save the resulting workbook to a file
workbook.SaveToFile("RemoveTable.xlsx", ExcelVersion.Version2016)
workbook.Dispose()
猜你喜欢
- 2025-03-18 如何在 Python 中进行平方:完整指南
- 2025-03-18 怎样让 Python 代码更简洁高效?这些实用技巧别错过!
- 2025-03-18 Python 一行代码帮你节省数小时工作
- 2025-03-18 实战指南:Python 代码优化的常见技巧与思路大集合
- 2025-03-18 Python 最大N个数与最小N个数的和
- 2025-03-18 Python高效办公:用自动化脚本批量处理Excel
- 2025-03-18 python基础函数(Python基础函数导入)
- 2025-03-18 Python 函数秘籍(Esoterica of Python Functions)
- 2025-03-18 别再死记硬背!Python函数知识点和练习题都在这里,速来挑战!
- 2025-03-18 Python函数:定义函数、参数传递、返回值、lambda 表达式
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)