通过Python编程实现Word文档中图片的动态管理,可精准实现图片的插入定位、条件化替换及冗余元素删除,尤其适用于需要定期生成标准化Word文档。这种自动化方案不仅显著降低人工操作导致的格式错位、版本混乱等风险,还能通过API接口与数据库、图像处理系统无缝集成,为构建端到端的智能文档生成体系奠定基础。本文将介绍如何使用Python在Word文档中添加、替换和删除图片。
本文所使用的方法需要用到免费的Free Spire.Doc for Python,PyPI:pip install spire.doc。
用Python插入图片到Word文档
我们可以通过库中提供的Paragraph.AppendPicture(string: fileName)方法从指定路径插入图片到Word文档中的指定段落,并可使用DocPicture类下的属性对图像的大小、位置和文字环绕方式等进行设置。以下是操作步骤:
- 导入所需模块。
- 创建
Document对象,从而新建Word文档。 - 使用
Document.AddSection()方法在文档中添加节,并设置格式。 - 使用
Section.AddParagraph()方法在节中添加段落,使用Paragraph.AppendText()方法在段落中添加文本,并设置格式。 - 使用
Paragraph.AppendPicture()方法在段落中添加图片。 - 使用
DocPicture类下的属性设置图片属性:- 使用
Width和Height属性设置图片大小。 - 使用
HorizontalPosition和VerticalPosition属性设置图片的位置。 - 使用
TextWrappingStyle属性设置图片的文字环绕方式。
- 使用
- 使用
Document.SaveToFile()方法保存文档。 - 释放资源。
代码示例:
from spire.doc import Document, TextWrappingStyle, FileFormat
# 创建Document对象,从而新建Word文档
doc = Document()
# 在文档中添加一个节,并设置页边距
section = doc.AddSection()
section.PageSetup.Margins.All = 72
# 在节中添加段落,在段落中添加文本
para = section.AddParagraph()
textRange = para.AppendText("Document Introduction")
textRange.CharacterFormat.FontName = "Arial"
textRange.CharacterFormat.FontSize = 16
textRange.CharacterFormat.Bold = True
# 添加段落,并添加图片到段落中
para1 = section.AddParagraph()
pic = para1.AppendPicture("Word.png")
# 设置图片尺寸
pic.Width = 64
pic.Height = 64
# 设置图片位置(可选)
pic.HorizontalPosition = 360
pic.VerticalPosition = 10
# 设置图片文本环绕方式
pic.TextWrappingStyle = TextWrappingStyle.Square
# 添加文本并设置格式
textRange2 = para1.AppendText("This document serves as a structured repository of critical information, designed to facilitate clear communication, collaborative workflows, and long-term knowledge retention. Its architecture prioritizes accessibility, with a navigable hierarchy of headings, cross-referenced sections, and embedded metadata to streamline information retrieval.")
textRange2.CharacterFormat.FontName = "Arial"
textRange2.CharacterFormat.FontSize = 12
# 保存文档
doc.SaveToFile("output/AddPicToWord.docx", FileFormat.Docx2019)
doc.Dispose()
结果文档

用Python替换Word文档中的图片为新的图片
我们可以通过判断段落的各个子对象的DocumentObjectType属性是否为DocumentObjectType.Picture来找出文档中的所有图片。然后,我们可以将图片子对象转换为DocPicture,再使用DocPicture.LoadImage()方法使用指定的图片替换原图片。替换后的图片将保留原图片的位置和文本环绕方式属性,图片大小可设置为原图片的大小。以下是操作步骤:
- 导入所需模块。
- 创建
Document对象,使用Document.LoadFromFile()方法载入Word文档。 - 依次遍历文档中的节、节中的段落以及段落中的子对象,判断每个子对象的
DocumentObjectType属性是否为DocumentObjectType.Picture,将判断成功的对象添加到列表中。 - 将图片子对象转换为
DocPicture,再使用DocPicture.LoadImage()方法使用指定的图片替换原图片。 - 使用
Document.SaveToFile()方法保存文档。 - 释放资源。
代码示例:
from spire.doc import Document, DocumentObjectType, DocPicture
# 创建Document对象
doc = Document()
# 加载Word文档
doc.LoadFromFile("output/AddPicToWord.docx")
# 创建一个列表来存储图片
pictures = []
# 遍历文档中的所有节
for i in range(doc.Sections.Count):
sec = doc.Sections.get_Item(i)
# 遍历每一节中的所有段落
for j in range(sec.Paragraphs.Count):
para = sec.Paragraphs.get_Item(j)
# 遍历每个段落中的所有子对象
for k in range(para.ChildObjects.Count):
docObj = para.ChildObjects.get_Item(k)
# 查找图片并将其添加到列表中
if docObj.DocumentObjectType == DocumentObjectType.Picture:
pictures.append(docObj)
# 用新图片替换列表中的第一张图片
picture = DocPicture(pictures[0])
width = picture.Width
height = picture.Height
picture.LoadImage("NewWord.png")
# 修改图片的尺寸
picture.Width = width
picture.Height = height
# 保存结果文档
doc.SaveToFile("output/ReplaceWordImage.docx")
doc.Close()
结果文档

用Python替换Word文档中的图片为文本
我们还可以在找出图片子对象后,使用Paragraph.ChildObjects.Insert(Index, TextRange)方法在其位置插入文本,并使用Paragraph.ChildObjects.Remove()方法将图片子对象删除,从而实现图片到文本的替换。以下是操作步骤:
- 导入所需模块。
- 创建
Document对象,使用Document.LoadFromFile()方法载入Word文档。 - 依次遍历文档中的节、节中的段落以及段落中的子对象,判断每个子对象的
DocumentObjectType属性是否为DocumentObjectType.Picture,将判断成功的对象添加到列表中。 - 通过创建
TextRange对象创建文本,设置文字及其格式。 - 使用
Paragraph.ChildObjects.IndexOf()方法获取图片子对象在段落中的索引。 - 使用
Paragraph.ChildObjects.Insert()方法将文本插入到段落中相应位置。 - 使用
Paragraph.ChildObjects.Remove()方法删除图片子对象。 - 使用
Document.SaveToFile()方法保存文档。 - 释放资源。
代码示例:
from spire.doc import Document, DocumentObjectType, TextRange, Color
# 创建Document对象
doc = Document()
# 加载Word文档
doc.LoadFromFile("output/AddPicToWord.docx")
# 遍历文档中的所有节
for k in range(doc.Sections.Count):
sec = doc.Sections.get_Item(k)
# 遍历每一节中的所有段落
for m in range(sec.Paragraphs.Count):
para = sec.Paragraphs.get_Item(m)
# 创建一个列表来存储图片
pictures = []
# 查找图片并将其添加到列表中
for x in range(para.ChildObjects.Count):
docObj = para.ChildObjects.get_Item(x)
if docObj.DocumentObjectType == DocumentObjectType.Picture:
pictures.append(docObj)
# 遍历列表中的所有图片,并用文本替换它们
for pic in pictures:
# 获取图片的索引位置
index = para.ChildObjects.IndexOf(pic)
# 创建一个TextRange对象
textRange = TextRange(doc)
textRange.Text = "Word Document "
textRange.CharacterFormat.FontName = "Arial"
textRange.CharacterFormat.FontSize = 20
textRange.CharacterFormat.TextColor = Color.get_Red()
# 将文本插入到段落中
para.ChildObjects.Insert(index, textRange)
# 删除图片
para.ChildObjects.Remove(pic)
# 保存结果文档
doc.SaveToFile("output/ReplaceWordImageWithText.docx")
doc.Close()
结果文档

用Python删除Word文档中的图片
在找出文档中的图片子对象后,我们可以直接使用Paragraph.ChildObjects.Remove()方法删除图片子对象,从而删除Word文档中的图片。以下是操作步骤:
- 导入所需模块。
- 创建
Document对象,使用Document.LoadFromFile()方法载入Word文档。 - 依次遍历文档中的节、节中的段落以及段落中的子对象,判断每个子对象的
DocumentObjectType属性是否为DocumentObjectType.Picture,将判断成功的对象添加到列表中。 - 使用
Paragraph.ChildObjects.IndexOf()方法获取图片子对象在段落中的索引。 - 使用
Paragraph.ChildObjects.Remove()方法删除图片子对象。 - 使用
Document.SaveToFile()方法保存文档。 - 释放资源。
代码示例:
from spire.doc import Document, DocumentObjectType, TextRange, Color
# 创建Document对象
doc = Document()
# 加载Word文档
doc.LoadFromFile("output/AddPicToWord.docx")
# 遍历文档中的所有节
for k in range(doc.Sections.Count):
sec = doc.Sections.get_Item(k)
# 遍历每一节中的所有段落
for m in range(sec.Paragraphs.Count):
para = sec.Paragraphs.get_Item(m)
# 创建一个列表来存储图片
pictures = []
# 查找图片并将其添加到列表中
for x in range(para.ChildObjects.Count):
docObj = para.ChildObjects.get_Item(x)
if docObj.DocumentObjectType == DocumentObjectType.Picture:
pictures.append(docObj)
# 遍历列表中的所有图片,并用文本替换它们
for pic in pictures:
# 获取图片的索引位置
index = para.ChildObjects.IndexOf(pic)
# 删除图片子对象
para.ChildObjects.Remove(pic)
# 保存结果文档
doc.SaveToFile("output/RemoveWordImage.docx")
doc.Close()
结果文档

本文演示了如何使用Python在Word文档中添加、替换以及删除图片,提供步骤介绍和代码示例。
1004

被折叠的 条评论
为什么被折叠?



