通过在 Word 表格中添加或删除行和列,用户可以调整表格的结构以有效地容纳您的数据。通过添加行和列,用户可以随着数据的增长毫不费力地扩展表,确保以全面的方式捕获和显示所有相关信息。另一方面,删除不必要的行和列可以简化表格,消除任何可能使文档混乱的冗余或无关数据。本文将介绍如何使用 Spire.Doc for Python 在 Python 中添加或删除表格行和列。
安装 Spire.Doc for Python
本教程需要使用 Spire.Doc for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Doc
如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.Doc for Python
Python 向 Word 表格添加或插入行
Spire.Doc for Python 提供了 Table.AddRow() 或 Table.InsertRow() 方法在 Word 表格的末尾添加一行或在 Word 表格的特定位置插入一行。下面是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 通过 Document.Sections[] 属性获取文档的第一部分。
- 通过 Section.Tables[] 属性获取该部分的第一个表。
- 使用 Table.Rows.Insert() 方法在表的特定位置插入一行。
- 将数据添加到新插入的行。
- 使用 Table.AddRow()方法在表的末尾添加一行。
- 将数据添加到新添加的行。
- 使用 Document.SaveToFile() 方法保存生成的文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 设置输入文件路径
inputFile = "data/createTable.docx"
# 创建文档对象
document = Document()
# 从输入文件中加载文档内容
document.LoadFromFile(inputFile)
# 获取文档的第一个节
section = document.Sections[0]
# 获取第一个节的第一个表格,如果不存在则返回None
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None
# 在表格的第2行插入新行
table.Rows.Insert(2, table.AddRow())
# 获取插入的行
insertedRow = table.Rows[2]
# 遍历插入行的每个单元格
for i in range(insertedRow.Cells.Count):
# 获取单元格
cell = insertedRow.Cells[i]
# 在单元格中添加段落
paragraph = cell.AddParagraph()
# 向段落中添加文本
paragraph.AppendText("插入行")
# 设置段落的水平对齐方式为居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# 设置单元格的垂直对齐方式为居中
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# 在表格末尾添加一行
addedRow = table.AddRow()
# 遍历新添加行的每个单元格
for i in range(addedRow.Cells.Count):
# 获取单元格
cell = addedRow.Cells[i]
# 在单元格中添加段落
paragraph = cell.AddParagraph()
# 向段落中添加文本
paragraph.AppendText("末尾行")
# 设置段落的水平对齐方式为居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# 设置单元格的垂直对齐方式为居中
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# 将修改后的文档保存到输出文件中
document.SaveToFile("output/AddRows.docx", FileFormat.Docx2016)
# 关闭文档对象
document.Close()
Python 向 Word 表格中添加或插入列
Spire.Doc for Python 不提供在 Word 表格中添加或插入列的直接方法。但是,用户可以通过使用 TableRow.Cells.Add() 或 TableRow.Cells.Insert() 方法在每个表格行的特定位置添加或插入单元格来实现此目的。下面是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 通过 Document.Sections[] 属性获取文档的第一部分。
- 通过 Section.Tables[] 属性获取该部分的第一个表。
- 遍历表的每一行。
- 创建一个 TableCell 对象,然后使用 TableRow.Cells.Insert() 方法将其插入每行的特定位置并设置单元格宽度。
- 向单元格添加数据并设置文本对齐方式。
- 使用 TableRow.AddCell() 方法在每行末尾添加一个单元格并设置单元格宽度。
- 向单元格添加数据并设置文本对齐方式。
- 使用 Document.SaveToFile() 方法保存生成的文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个Document对象
document = Document()
# 从"Table1.docx"文件中加载文档内容
document.LoadFromFile("data/Table.docx")
# 获取文档的第一个节(Section)
section = document.Sections[0]
# 获取第一个节中的第一个表格,如果该表格存在的话
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None
# 遍历表格的每一行
for i in range(table.Rows.Count):
# 获取当前行
row = table.Rows[i]
# 创建一个新的单元格,并将其插入到当前行的第三个位置
cell = TableCell(document)
row.Cells.Insert(2, cell)
# 设置新单元格的宽度与第一列相同
cell.Width = row.Cells[0].Width
# 在新单元格中添加一个段落,并设置文本内容为"Inserted Column"
paragraph = cell.AddParagraph()
paragraph.AppendText("插入列")
# 设置段落的水平对齐方式为居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# 设置新单元格的垂直对齐方式为居中
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# 在当前行的末尾添加一个新的单元格
cell = row.AddCell()
# 设置新单元格的宽度与第二列相同
cell.Width = row.Cells[1].Width
# 在新单元格中添加一个段落,并设置文本内容为"End Column"
paragraph = cell.AddParagraph()
paragraph.AppendText("末尾列")
# 设置段落的水平对齐方式为居中
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
# 设置新单元格的垂直对齐方式为居中
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
# 将修改后的文档保存为"AddColumns.docx"文件,格式为Docx2016
document.SaveToFile("output/AddColumns.docx", FileFormat.Docx2016)
# 关闭文档对象
document.Close()
Python 从 Word 表格中删除行
Spire.Doc for Python 提供了 Table.Rows.RemoveAt() 方法,从 Word 表中删除特定行。下面是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 通过 Document.Sections[] 属性获取文档的第一部分。
- 通过 Section.Tables[] 属性获取该部分的第一个表。
- 使用 Table.Rows.RemoveAt() 方法从表中删除特定行。
- 使用 Document.SaveToFile() 方法保存生成的文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个Document对象
document = Document()
# 从"AddRows.docx"文件中加载文档内容
document.LoadFromFile("data/AddRows.docx")
# 获取文档的第一个节(Section)
section = document.Sections[0]
# 获取第一个节中的第一个表格,如果该表格存在的话
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None
# 移除表格中的第1行
table.Rows.RemoveAt(0)
# 移除表格中的最后一行
table.Rows.RemoveAt(table.Rows.Count - 1)
# 将修改后的文档保存为"RemoveRows.docx"文件,格式为Docx2016
document.SaveToFile("output/RemoveRows.docx", FileFormat.Docx2016)
# 关闭文档对象
document.Close()
Python 从 Word 表格中删除列
Spire.Doc for Python 提供了 TableRow.Cells.RemoveAt() 这个方法从每个表格行中删除相应的单元格。下面是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 通过 Document.Sections[] 属性获取文档的第一部分。
- 通过 Section.Tables[] 属性获取该部分的第一个表。
- 遍历表的每一行。
- 使用 TableRow.Cells.RemoveAt() 方法从每行中删除特定单元格。
- 使用 Document.SaveToFile() 方法保存生成的文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个Document对象
document = Document()
# 从"AddColumns.docx"文件中加载文档内容
document.LoadFromFile("data/AddColumns.docx")
# 获取文档的第一个节(Section)
section = document.Sections[0]
# 获取第一个节中的第一个表格,如果该表格存在的话
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None
# 遍历表格的每一行
for i in range(table.Rows.Count):
# 获取当前行
row = table.Rows[i]
# 移除第1个单元格
row.Cells.RemoveAt(0)
# 移除最后一个单元格
row.Cells.RemoveAt(row.Cells.Count - 1)
# 将修改后的文档保存为"RemoveColumns.docx"文件,格式为Docx2016
document.SaveToFile("output/RemoveColumns.docx", FileFormat.Docx2016)
# 关闭文档对象
document.Close()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。