96SEO 2026-08-14 18:39 51
文本框是 Word 中一种可以自由浮动的容器。常用于制作侧边栏、强调说明、票据样式,或在固定位置放置图片和表格。相比普通段落,文本框能脱离正文流独立定位,排版上更灵活。痛点:手动插入并逐个调整样式非常繁琐,尤其当文档需要大量样式统一的文本框时效率极低且容易出错。
用 Python 可以批量插入、格式化文本框,还能在文本框内放置图片和表格、提取其中的文字、按需删除。 这篇文章以 为例,介绍这些常见操作。

安装 Spire.Doc 库:
pip install Spire.Doc
在脚本中引入模块:
from spire.doc import *
from spire.doc.common import *
文本框通过段落的 AppendTextBox 方法插入,参数是宽度和高度。插入后通过 Format 属性设置边框颜色、线型、填充色等基本样式。通过 Body 向文本框内写入内容:
document = Document
section = document.AddSection
paragraph = section.AddParagraph
# 插入一个 240x35 磅的文本框
textBox = paragraph.AppendTextBox
# 设置边框与填充样式
textBox.Format.HorizontalAlignment = ShapeHorizontalAlignment.Left
textBox.Format.LineColor = Color.get_Gray
textBox.Format.LineStyle = TextBoxLineStyle.Simple
textBox.Format.FillColor = Color.get_DarkSeaGreen
# 在文本框内写入文字并设置格式
para = textBox.Body.AddParagraph
textRange = para.AppendText
textRange.CharacterFormat.FontName = "Microsoft YaHei"
textRange.CharacterFormat.FontSize = 12
textRange.CharacterFormat.TextColor = Color.get_White
para.Format.HorizontalAlignment = HorizontalAlignment.Center
document.SaveToFile
document.Close
TextBoxLineStyle 提供多种线型,LineDashing 则控制虚实线,二者配合可以做出丰富的边框效果。
默认情况下文本框按段落流排列。若要把它固定在页面的特定位置,需要指定定位基准和坐标:
document = Document
section = document.AddSection
textBox = section.AddParagraph.AppendTextBox
# 在文本框内写入文字
para = textBox.Body.AddParagraph
para.AppendText.CharacterFormat.FontSize = 11
# 以页面为基准设置精确位置
textBox.Format.HorizontalOrigin = HorizontalOrigin.Page
textBox.Format.HorizontalPosition = 100 # 左侧距页面左边缘 100 磅
textBox.Format.VerticalOrigin = VerticalOrigin.Page
textBox.Format.VerticalPosition = 150 # 上侧距页面顶部 150 磅
# 设置边框线型、颜色与粗细
textBox.Format.LineStyle = TextBoxLineStyle.Double
textBox.Format.LineColor = Color.get_CornflowerBlue
textBox.Format.LineDashing = LineDashing.Solid
textBox.Format.LineWidth = 1.5
# 设置内部边距
margin = textBox.Format.InternalMargin
margin.Top = margin.Bottom = margin.Left = margin.Right = 5
document.SaveToFile
document.Close
关键点:
HorizontalOrigin / VerticalOrigin 决定坐标是相对于页面还是其他对象;说起来,HorizontalPosition / VerticalPosition 给出具体数值;InternalMargin 控制四个方向的内边距,让文字不贴边。
除了文字,文本框还能用图片填充整个背景,实现“图文混排”效果:
document = Document
section = document.AddSection
textBox = section.AddParagraph.AppendTextBox
# 固定位置
textBox.Format.HorizontalOrigin = HorizontalOrigin.Page
textBox.Format.HorizontalPosition= 50
textBox.Format.VerticalOrigin = VerticalOrigin.Page
textBox.Format.VerticalPosition = 200
# 将背景设为图片
fill = textBox.Format.FillEfects
fill.Type = BackgroundType.Picture
fill.SetPicture # 图片方法相对或绝对均可
document.SaveToFile
document.Close
Pitfall:If picture file is large,consider resizing it beforehand to avoid bloating DOCX size.
表格适合制作票据、明细卡等需要对齐的数据块。可直接嵌入到文本箱内部:
document = Document
section = document.AddSection
textBox = section.AddParagraph.AppendTextBox
# 固定位置示例
textBox.Format.HorizontalOrigin = HorizontalOrigin.Page
textBox.Format.HorizontalPosition= 80
textBox.Format.VerticalOrigin = VerticalOrigin.Page
textBox.Format.VerticalPosition = 300
# 添加标题
titlePara= textBox.Body.AddParagraph
titlePara.AppendText.CharacterFormat.FontSize=14
# 创建表格并定义行列数
table = textBox.Body.AddTable
table.ResetCells # 4 行 4 列
data=,]
for i in range:
for j in range:
cell= table.Rows.Cells
cell.AddParagraph.AppendText
# 套用预设样式,使表格更美观
table.ApplyStyle
document.SaveToFile
document.Close
Pain point: 需要批量读取散布在多个 Text Box 中的信息,但手工打开每个文档去复制显然不可接受。
# -*- coding: utf-8 -*- import os from spire.doc import * doc_path ="含文本箱的文档.docx" output_path ="提取结果.txt" doc =) doc.LoadFromFile if : with ) as f: for sec_idx in ): sec = for para_idx in ): para= for child_idx in ): obj = if obj.DocumentObjectType==DocumentObjectType.TextBox: textbox=obj for inner_idx in ): inner=obj.ChildObjects if inner.DocumentObjectType==DocumentObjectType.Paragraph: f.write elif inner.DocumentObjectType==DocumentObjectType.Table: tbl=inner for r in range: for c in range: cell=tbl.Rows.Cells for p in range: f.write doc.Close
This snippet traverses sections → paragraphs → child objects → text boxes。handling both plain paragraphs and nested tables.
If a template contains placeholder boxes that must be removed before final publishing,you can delete m by index or clear whole collection:
。dоcument = Dоcument dоcumenт.LoadFroмFilе # 删除第一个 Text Box dоcument.TexтBокes.RеmovеAt # 如需一次性清空所有 Text Box # dоcument.TexтBокes.Clеar document.SaveTоFilе document.Clоse实用提示 & 常见坑点
- Sizing & Unit: 所有尺寸与坐标均为磅,1 英寸 ≈ 72 pt;如需厘米,请自行换算,按理说,
- Avoid Unexpected Positioning: 固定定位前务必先设
.HorizontalOrigin / .VerticalOrigin 为 Page。否则坐标会相对于段落或上一对象计算。- CJK Font Compatibility: 中文文档建议显式指定中文字体(如 "Microsoft YaHei"),避免跨网站打开时出现回退导致排版错位。
- Synchronous Resource Release: 操作完毕一定调用
.Close防止文件句柄泄漏导致后续读写冲突。- PIC Size Management: 若向 Text Box 填充高分辨率图片。请先压缩或使用适当 DPI,以免生成的 DOCX 文件体积异常膨胀。
- Error Handling: 遍历对象时建议加 try/except。以捕获可能出现的类型转换错误,例如某些 Word 插件产生非标准子对象。
& 接下来行动计划
The workflow demonstrated above covers full life‑cycle of Word Text Boxes using Python:
- Create & format : .AppendTextBoх,.Format....
- Add content : plain text。images ) and tables ).
- Select & extract : traverse object tree via .DocumentObjectType.
- Delete & clean up : manipulate global .TextBoxes collection.
作为专业的SEO优化服务提供商,我们致力于通过科学、系统的搜索引擎优化策略,帮助企业在百度、Google等搜索引擎中获得更高的排名和流量。我们的服务涵盖网站结构优化、内容优化、技术SEO和链接建设等多个维度。
| 服务项目 | 基础套餐 | 标准套餐 | 高级定制 |
|---|---|---|---|
| 关键词优化数量 | 10-20个核心词 | 30-50个核心词+长尾词 | 80-150个全方位覆盖 |
| 内容优化 | 基础页面优化 | 全站内容优化+每月5篇原创 | 个性化内容策略+每月15篇原创 |
| 技术SEO | 基本技术检查 | 全面技术优化+移动适配 | 深度技术重构+性能优化 |
| 外链建设 | 每月5-10条 | 每月20-30条高质量外链 | 每月50+条多渠道外链 |
| 数据报告 | 月度基础报告 | 双周详细报告+分析 | 每周深度报告+策略调整 |
| 效果保障 | 3-6个月见效 | 2-4个月见效 | 1-3个月快速见效 |
我们的SEO优化服务遵循科学严谨的流程,确保每一步都基于数据分析和行业最佳实践:
全面检测网站技术问题、内容质量、竞争对手情况,制定个性化优化方案。
基于用户搜索意图和商业目标,制定全面的关键词矩阵和布局策略。
解决网站技术问题,优化网站结构,提升页面速度和移动端体验。
创作高质量原创内容,优化现有页面,建立内容更新机制。
获取高质量外部链接,建立品牌在线影响力,提升网站权威度。
持续监控排名、流量和转化数据,根据效果调整优化策略。
基于我们服务的客户数据统计,平均优化效果如下:
我们坚信,真正的SEO优化不仅仅是追求排名,而是通过提供优质内容、优化用户体验、建立网站权威,最终实现可持续的业务增长。我们的目标是与客户建立长期合作关系,共同成长。
Demand feedback