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 天的临时许可证。