在当今自动化办公需求日益增长的背景下,通过编程手段动态管理Word文档中的文本框元素已成为提升工作效率的关键技术路径。文本框作为文档排版中灵活的内容容器,既能承载多模态信息(如文字、图像),又可实现独立于正文流的位置调整与样式定制,但其手动操作存在重复性高、批量处理困难等痛点。通过Python脚本对文本框进行精准控制,不仅能够实现跨文档的批量插入与删除操作,还能将复杂布局设计与数据动态绑定,例如自动生成报告模板、标准化企业文档格式或构建交互式内容系统。本文将介绍如何使用Python在Word文档中插入和删除文本框

  • 用Python插入文本框到Word文档
  • 用Python删除Word文档中的文本框

本文所使用的方法需要用到Free Spire.Doc for Python,PyPI:pip install spire.doc.free

用Python插入文本框到Word文档

我们可以使用库中提供的Workbook类来创建或载入Word文档,然后使用Paragraph.AppendTextBox(width: float, height: float)方法在段落中添加任意尺寸的文本框。其中,TextBox.Format属性可对文本框格式进行设置,同时我们可以在文本框中添加文本、图片等元素。
以下是操作步骤:

  1. 创建Document实例,以新建Word文档。
  2. 使用Document.AddSection()方法在文档中添加一个节,并设置页面。
  3. 使用Section.AddParagraph()方法在节中添加一个段落,并插入内容。
  4. 使用Paragraph.AppendTextBox()方法,在段落中添加一个指定尺寸的文本框。
  5. 使用TextBox.Format下的属性设置文本框格式。
  6. 使用TextBox.Body.AddParagraph()方法在文本框中添加段落。
  7. 在文本框段落中添加内容并设置格式。
  8. 使用Document.SaveToFile()方法保存Word文档。

代码示例

from spire.doc import Document, VerticalOrigin, TextWrappingStyle, HorizontalOrigin, BackgroundType, Color, TextBoxLineStyle, LineDashing, HorizontalAlignment, FileFormat

# 创建文档对象
document = Document()

# 添加节
section = document.AddSection()
section.PageSetup.Margins.Top = 50
section.PageSetup.Margins.Bottom = 50
# 添加段落
main_paragraph = section.AddParagraph()
text_range = main_paragraph.AppendText("以下是一个文本框示例:\r\n")
text_range.CharacterFormat.FontName = "微软雅黑"
text_range.CharacterFormat.FontSize = 14

# 在段落中添加一个文本框
textbox = main_paragraph.AppendTextBox(300, 200)  # 设置文本框尺寸

# 设置文本框位置和环绕样式
textbox.Format.HorizontalOrigin = HorizontalOrigin.Page
textbox.Format.HorizontalPosition = 50
textbox.Format.VerticalOrigin = VerticalOrigin.Page
textbox.Format.VerticalPosition = 80
textbox.Format.TextWrap = TextWrappingStyle.InFrontOfText

# 设置渐变背景填充
textbox.Format.FillEfects.Type = BackgroundType.Gradient
textbox.Format.FillEfects.Gradient.Color1 = Color.get_BlueViolet()
textbox.Format.FillEfects.Gradient.Color2 = Color.get_LightSkyBlue()
textbox.Format.FillEfects.Gradient.Angle = 45

# 设置三维边框样式
textbox.Format.LineColor = Color.get_DarkBlue()
textbox.Format.LineStyle = TextBoxLineStyle.ThickThin
textbox.Format.LineWidth = 3.5
textbox.Format.LineDashing = LineDashing.DashDot

# 在文本框内添加带图标的标题段落
header_paragraph = textbox.Body.AddParagraph()
icon = header_paragraph.AppendPicture("alert.png")
icon.Width = 20
icon.Height = 20

title_text = header_paragraph.AppendText(" 重要通知")
title_text.CharacterFormat.FontName = "微软雅黑"
title_text.CharacterFormat.FontSize = 14
title_text.CharacterFormat.TextColor = Color.get_Gold()

# 添加分隔线
separator = textbox.Body.AddParagraph()
separator.Format.Borders.Top.Color = Color.get_Gold()
separator.Format.Borders.Top.LineWidth = 2
separator.Format.Borders.Top.Space = 5

# 添加图文混排内容
content_paragraph = textbox.Body.AddParagraph()
image = content_paragraph.AppendPicture("document.png")
image.Width = 60
image.Height = 60
image.TextWrappingStyle = TextWrappingStyle.Through

text_content = content_paragraph.AppendText("本通知包含重要更新内容,请仔细阅读。如需进一步操作,请访问我们的知识库或联系技术支持。")
text_content.CharacterFormat.FontName = "黑体"
text_content.CharacterFormat.FontSize = 12
text_content.CharacterFormat.TextColor = Color.get_WhiteSmoke()

# 添加旋转文字装饰
rotated_paragraph = textbox.Body.AddParagraph()
rotated_text = rotated_paragraph.AppendText("机密文件")
rotated_text.CharacterFormat.FontName = "HarmonyOS Sans SC"
rotated_text.CharacterFormat.FontSize = 24
rotated_text.CharacterFormat.TextColor = Color.FromArgb(100, 255, 255, 255)
rotated_paragraph.Format.Rotation = 30  # 30度旋转
rotated_paragraph.Format.HorizontalAlignment = HorizontalAlignment.Right

# 保存文档
document.SaveToFile("output/TextBox.docx", FileFormat.Docx2019)
document.Close()

结果文档
Python插入文本框到Word文档

用Python删除Word文档中的文本框

我们可以用Document.TextBoxes属性来获取文档中的所有文本框,然后使用RemoveAt()方法根据参数删除指定文本框,或直接使用Clear()方法删除所有文本框。
以下是操作步骤:

  1. 创建Document对象,使用Document.LoadFromFile()方法载入Word文档。
  2. 使用Document.TextBoxes获取文档中的所有文本框。
  3. 使用TextBoxCollection.RemoveAt()方法删除指定文本框,或使用TextBoxCollection.Clear()方法删除所有文本框。
  4. 使用Document.SaveToFile()方法保存Word文档。

代码示例

from spire.doc import Document

# 创建Document对象
document = Document()

# 载入Word文档
document.LoadFromFile("output/TextBox.docx")

# 删除指定文本框
document.TextBoxes.RemoveAt(0)

# 删除所有文本框
document.TextBoxes.Clear()

# 保存文档
document.SaveToFile("output/RemoveTextBox.docx")
document.Close()

结果文档
Python删除Word文档中的文本框

本文演示如何使用Python在Word文档中插入和删除文本框,提供操作步骤和代码示例。


大丸子
72 声望7 粉丝