Excel 和 Word 是日常办公中常用的两种文档格式。Excel 更适合处理复杂的数据计算与分析,而 Word 则擅长排版和增强文档的视觉效果。在制作报告或演示文档时,许多专业人士需要将 Excel 数据转入 Word 表格,以利用 Word 的格式化功能,从而提升文档的专业性和可读性。这篇文章将介绍如何使用 Spire.Office for Python 在 Python 中将 Excel 数据转换为 Word 表格并保留格式。
安装 Spire.Office for Python
本教程需要 Spire.Office for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Office
Python 将 Excel 数据转换为 Word 表格并保留格式
该功能用到了 Spire.Office for Python 包中的两个库:Spire.XLS for Python 和 Spire.Doc for Python。前者用于读取 Excel 工作表中的数据和格式,后者用于创建 Word 文档并将数据(包括格式)写入表格。为了使代码示例易于理解,我们创建了以下两个自定义方法来实现特定的功能:
- MergeCells() - 根据 Excel 工作表中的合并单元格来合并 Word 表格中的相应单元格。
- CopyStyle() - 将 Excel 工作表中的各种单元格样式复制到 Word 表格中,包括字体样式、背景颜色和文本对齐方式。
以下步骤展示了如何使用 Spire.Office for Python 将 Excel 工作表中的数据转换为Word 表格并保留格式:
- 创建 Workbook 类的对象,并使用 Workbook.LoadFromFile() 方法加载示例 Excel 文件。
- 通过 Workbook.Worksheets[index] 属性获取特定工作表。
- 使用 Document 类创建一个新的 Word 文档,并向其中添加一个Section。
- 使用 Section.AddTable() 方法向 Word 文档添加一个表格。
- 使用自定义方法 MergeCells() 检测 Excel 工作表中的合并单元格,并在 Word 表格中合并相应的单元格。
- 遍历 Excel 工作表中的单元格,通过 CellRange.Value 属性读取单元格的数据,并使用 TableCell.AddParagraph().AppendText() 方法将数据添加到 Word 表格的单元格中。
- 使用自定义方法 CopyStyle() 将 Excel 工作表中的单元格样式复制到 Word 表格中。
- 使用 Document.SaveToFile() 方法保存 Word 文档。
- Python
from spire.xls import *
from spire.doc import *
def MergeCells(sheet, table):
"""根据 Excel 工作表中的合并单元格合并 Word 表格中的对应单元格"""
if sheet.HasMergedCells:
ranges = sheet.MergedCells
for i in range(len(ranges)):
startRow = ranges[i].Row
startColumn = ranges[i].Column
rowCount = ranges[i].RowCount
columnCount = ranges[i].ColumnCount
if rowCount > 1 and columnCount > 1:
for j in range(startRow, startRow + rowCount):
table.ApplyHorizontalMerge(j - 1, startColumn - 1, startColumn - 1 + columnCount - 1)
table.ApplyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount - 1)
if rowCount > 1 and columnCount == 1:
table.ApplyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount - 1)
if columnCount > 1 and rowCount == 1:
table.ApplyHorizontalMerge(startRow - 1, startColumn - 1, startColumn - 1 + columnCount - 1)
def CopyStyle(wTextRange, xCell, wCell):
"""将单元格样式从 Excel 复制到 Word"""
# 复制字体样式
wTextRange.CharacterFormat.TextColor = Color.FromRgb(xCell.Style.Font.Color.R, xCell.Style.Font.Color.G, xCell.Style.Font.Color.B)
wTextRange.CharacterFormat.FontSize = float(xCell.Style.Font.Size)
wTextRange.CharacterFormat.FontName = xCell.Style.Font.FontName
wTextRange.CharacterFormat.Bold = xCell.Style.Font.IsBold
wTextRange.CharacterFormat.Italic = xCell.Style.Font.IsItalic
# 复制背景颜色
if xCell.Style.FillPattern is not ExcelPatternType.none:
wCell.CellFormat.BackColor = Color.FromRgb(xCell.Style.Color.R, xCell.Style.Color.G, xCell.Style.Color.B)
# 复制水平对齐方式
if xCell.HorizontalAlignment == HorizontalAlignType.Left:
wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left
elif xCell.HorizontalAlignment == HorizontalAlignType.Center:
wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
elif xCell.HorizontalAlignment == HorizontalAlignType.Right:
wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right
# 复制垂直对齐方式
if xCell.VerticalAlignment == VerticalAlignType.Bottom:
wCell.CellFormat.VerticalAlignment = VerticalAlignment.Bottom
elif xCell.VerticalAlignment == VerticalAlignType.Center:
wCell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
elif xCell.VerticalAlignment == VerticalAlignType.Top:
wCell.CellFormat.VerticalAlignment = VerticalAlignment.Top
# 加载 Excel 文件
workbook = Workbook()
workbook.LoadFromFile("Contact list.xlsx")
# 获取第一个工作表
sheet = workbook.Worksheets[0]
# 创建 Word 文档
doc = Document()
section = doc.AddSection()
section.PageSetup.Orientation = PageOrientation.Landscape
# 添加表格
table = section.AddTable(True)
table.ResetCells(sheet.LastRow, sheet.LastColumn)
# 根据 Excel 工作表中的合并单元格合并 Word 表格中的对应单元格
MergeCells(sheet, table)
# 从 Excel 导出数据和单元格样式到 Word 表格
for r in range(1, sheet.LastRow + 1):
table.Rows[r - 1].Height = float(sheet.Rows[r - 1].RowHeight)
for c in range(1, sheet.LastColumn + 1):
xCell = sheet.Range[r, c]
wCell = table.Rows[r - 1].Cells[c - 1]
# 复制数据
textRange = wCell.AddParagraph().AppendText(xCell.NumberText)
# 复制单元格样式
CopyStyle(textRange, xCell, wCell)
# 将 Word 文档保存到文件
doc.SaveToFile("Excel转Word表格.docx", FileFormat.Docx)
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。