使用 Python 将 Word 表格导出为 Excel 工作表【单个与批量操作】
Word 经常用于创建包含表格的报告、清单和业务文档,但如果需要进一步统计数据、筛选记录或进行计算,将表格内容转移到 Excel 中通常更加方便。手动复制粘贴虽然简单,但当文档较多或表格较复杂时,处理起来就比较耗时。
使用 Python,可以通过 Spire.Doc 读取 Word 文档中的表格,再借助 Spire.XLS 将单元格数据写入 Excel 工作表。本文将介绍两种常见情况:将 Word 中的单个表格导出为 Excel,以及将多个表格分别导出到不同的工作表中。
安装所需的 Python 库
本文使用以下两个库:
- Spire.Doc for Python:用于加载 Word 文档、访问文档中的表格,以及提取行和单元格内容。
- Spire.XLS for Python:用于创建 Excel 工作簿、添加工作表和写入单元格数据。
可以使用 pip 安装:
pip install Spire.Doc
pip install Spire.XLS
安装完成后,就可以在 Python 脚本中导入相应模块。
将 Word 中的单个表格导出为 Excel
假设有一份 Word 文档,其中包含一张需要导出的数据表。我们可以使用 Spire.Doc 加载文档,通过 Sections 和 Tables 集合获取目标表格,再遍历其中的行和单元格,最后使用 Spire.XLS 将提取的数据写入 Excel。
实现步骤:
- 创建
Document对象,使用LoadFromFile()加载 Word 文档。 - 使用
doc.Sections.get_Item(0)获取第一个 Section,再通过section.Tables.get_Item(0)获取其中的第一张表格。 - 使用
table.Rows.Count获取表格行数,通过row.Cells.Count获取每行的单元格数量,并使用cell.Paragraphs提取单元格中的文本。 - 创建
Workbook对象,通过workbook.Worksheets[0]获取默认工作表,再使用sheet.SetCellValue()将提取的数据写入对应单元格。 - 调用
workbook.SaveToFile()将工作簿保存为 XLSX 文件。
完整代码:
from spire.doc import Document
from spire.xls import *
from spire.xls.common import *
# Define input and output paths
inputFile = "E:/Administrator/Python/input/Sample.docx"
outputFile = "E:/Administrator/Python/output/WordTableToExcel.xlsx"
# Load the Word document
doc = Document()
doc.LoadFromFile(inputFile)
# Get the first table in the first section
section = doc.Sections.get_Item(0)
table = section.Tables.get_Item(0)
# Create an Excel workbook
workbook = Workbook()
sheet = workbook.Worksheets[0]
# Iterate through the rows and cells of the Word table
for rowIndex in range(table.Rows.Count):
row = table.Rows.get_Item(rowIndex)
for cellIndex in range(row.Cells.Count):
cell = row.Cells.get_Item(cellIndex)
# Extract text from all paragraphs in the cell
cellText = ""
for i in range(cell.Paragraphs.Count):
cellText += cell.Paragraphs.get_Item(i).Text
# Write the text to the corresponding Excel cell
sheet.SetCellValue(rowIndex + 1, cellIndex + 1, cellText)
# Save the Excel workbook
workbook.SaveToFile(outputFile, FileFormat.Version2016)
# Release resources
doc.Close()
workbook.Dispose()
运行脚本后,Word 文档中第一张表格的数据就会被写入 Excel 工作表。需要注意,这种方式主要提取单元格文本和行列数据,不会自动保留 Word 中的字体、边框、背景色等原始格式。
将 Word 中的多个表格导出到不同的 Excel 工作表
如果 Word 文档中包含多张表格,只导出第一张显然无法满足需求。例如,一份业务报告可能分别使用不同的表格记录销售数据、客户信息和订单明细。为了方便后续整理,我们可以将每张表格分别写入 Excel 工作簿中的独立工作表。
与前面的方法相比,主要变化在于遍历方式:不再直接获取第一张表格,而是依次遍历文档中的所有 Section 及其包含的表格,并为每张表格创建一个新的工作表。
实现步骤:
- 创建
Document对象并使用LoadFromFile()加载 Word 文档。 - 使用
doc.Sections.Count获取文档中的 Section 数量,通过循环遍历每个 Section。 - 使用
section.Tables.Count获取当前 Section 中的表格数量,再通过循环逐一访问表格。 - 使用
workbook.Worksheets.Add()为每张表格创建独立的 Excel 工作表,并设置工作表名称。 - 遍历当前表格中的行和单元格,使用
SetCellValue()将单元格文本写入对应工作表。 - 所有表格处理完成后,调用
workbook.SaveToFile()保存 Excel 文件。
完整代码:
from spire.doc import Document
from spire.xls import *
from spire.xls.common import *
# Define input and output paths
inputFile = "E:/Administrator/Python/input/Sample.docx"
outputFile = "E:/Administrator/Python/output/MultipleTablesToExcel.xlsx"
# Load the Word document
doc = Document()
doc.LoadFromFile(inputFile)
# Create an Excel workbook and clear the default worksheets
workbook = Workbook()
workbook.Worksheets.Clear()
# Iterate through all sections in the Word document
for sectionIndex in range(doc.Sections.Count):
section = doc.Sections.get_Item(sectionIndex)
# Iterate through all tables in the current section
for tableIndex in range(section.Tables.Count):
table = section.Tables.get_Item(tableIndex)
# Create a separate worksheet for the current table
sheetName = f"Table_{sectionIndex + 1}_{tableIndex + 1}"
sheet = workbook.Worksheets.Add(sheetName)
# Iterate through the rows and cells
for rowIndex in range(table.Rows.Count):
row = table.Rows.get_Item(rowIndex)
for cellIndex in range(row.Cells.Count):
cell = row.Cells.get_Item(cellIndex)
# Extract text from all paragraphs in the cell
cellText = ""
for i in range(cell.Paragraphs.Count):
cellText += cell.Paragraphs.get_Item(i).Text
# Write the text to the corresponding worksheet cell
sheet.SetCellValue(rowIndex + 1, cellIndex + 1, cellText)
# Save the Excel workbook
workbook.SaveToFile(outputFile, FileFormat.Version2016)
# Release resources
doc.Close()
workbook.Dispose()
通过这种方式,文档中的每张表格都会被写入一个独立的工作表。例如,第一节中的两张表格会分别保存为 Table_1_1 和 Table_1_2,第二节中的表格则会使用 Table_2_1 等名称。这样可以避免不同表格的数据混在一起,也方便后续查找和编辑。
扩展:批量导出多个 Word 文档中的表格
如果需要处理的不是一份 Word 文档,而是整个文件夹中的多份文档,也可以在上述方法的基础上进行扩展。
例如,可以使用 Python 内置的 os 模块遍历指定文件夹,筛选其中的 .docx 和 .doc 文件,然后逐个加载文档并执行表格提取操作。
在这种情况下,可以根据原始 Word 文件名生成对应的 Excel 文件名,并将每份文档中的表格分别导出。这样就能实现整个文件夹的自动化处理,而不必逐一打开 Word 文档进行复制和整理。
总结
将 Word 表格导出为 Excel,可以减少手动复制数据的工作量,也方便后续进行数据统计、筛选和分析。对于只需要处理一张表格的情况,可以直接获取目标表格并写入工作表;如果文档中包含多张表格,则可以遍历所有 Section 和 Tables 集合,将它们分别保存到独立的 Excel 工作表中。
借助 Spire.Doc 和 Spire.XLS,还可以将这一过程集成到 Python 自动化工作流中,进一步处理批量文档和重复性数据提取任务。无论是日常办公文档整理,还是需要定期处理大量 Word 报告,都可以根据实际需求调整导出逻辑,提高文档数据处理效率。

浙公网安备 33010602011771号