专业编程基础技术教程

网站首页 > 基础教程 正文

如何使用 Python 在 Excel 中创建、更新和删除表格

ccvgpt 2025-03-18 20:10:07 基础教程 2 ℃

Excel 中的表格是一个强大的工具,可以帮助我们有效地管理、组织和分析数据。通过将数据范围转换为表格,我们可以轻松地对数据进行筛选、排序和计算

用于在 Excel 中处理表格的 Python 库

Spire.XLS for Python 提供了用于处理 Excel 文件的广泛功能。我们可以使用它来创建、修改和删除 Excel 文档中的表格、图表和许多其他对象。此外,Spire.XLS 支持多种 Excel 格式,例如 .xls、.xlsx、.xlsm、.xlsb 等,使其成为 Python 中数据处理任务的多功能工具。

如何使用 Python 在 Excel 中创建、更新和删除表格

安装

我们可以通过运行以下命令从 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] 属性进行访问。访问后,我们可以使用 DisplayNameLocation 属性更改其名称和数据范围。

以下是如何使用 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()

最近发表
标签列表