python批量调整word含有关键字句子样式

代码:

python 复制代码
from docx import Document
from docx.shared import RGBColor
obj= Document('装机必读调整样式.docx')
word ='软件'
def set_run(run):
   #设置run的字体大小、是否加粗以及字体颜色
   run.font.size = font_size
   run.bold = bold
   run.font.color.rgb= color
for p in obj.paragraphs:
   for r in p.runs:
       if word not in r.text:
           pass
       #获取当前run的字体属性
       font_size =r.font.size
       bold =r.bold
       color =r.font.color.rgb
       #使用关键词切分当前run的文本
       rest =r.text.split(word)
       #清除当前run的内容
       r.text = ''
       for text in rest[:-1]:
           run = p.add_run(text=text)
           set_run(run)
           run = p.add_run(word)
           run.font.size = font_size
           run.bold = bold
           run.font.color.rgb=RGBColor(255,0,0)
       run = p.add_run(rest[-1])
       set_run(run)
   obj.save('装机必读调整之后样式.docx')

执行结果:

相关推荐
心流剑1 分钟前
从零打造带降级路由的 Agentic RAG 校园智能体(附前端终端UI实战)
python·ai·rag
天远数科1 小时前
零信任架构实战:基于天远车辆过户详版查询构建自动化抵押车权属合规审查网关
人工智能·python·架构·自动化
zbguolei1 小时前
第002篇 · 战火中的电子黎明
开发语言·php
sean_wang121 小时前
Rust 轻量级虚拟机 smolvm 实战:200ms 冷启动、OCI 镜像直跑、单文件打包
开发语言·后端·rust·大鲸华章
badhope1 小时前
3 个 Agent 并行发文章:我用 SQLite 任务表止住重复
后端·python·ai编程
战6172 小时前
Python 基础算法:三种排序、二分查找与递归
开发语言·python·算法
databook2 小时前
马尔可夫链入门:状态、转移概率与无记忆性
python·机器学习·数据分析
郝学胜-神的一滴2 小时前
量化交易从入门到精通 05:策略从哪来
开发语言·python·numpy·pandas·scikit-learn·matplotlib·scipy
zbguolei3 小时前
第001篇 · 算盘与差分机
开发语言·php