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')

执行结果:

相关推荐
梦想不只是梦与想28 分钟前
Python 中的装饰器
python·装饰器
我叫唧唧波1 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
AAA大运重卡何师傅(专跑国道)1 小时前
【无标题】
开发语言·c#
copyer_xyf2 小时前
Python 异常处理
前端·后端·python
XBodhi.2 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
麻雀飞吧2 小时前
期货多合约策略目标持仓怎么更新才不乱
python·区块链
Cthy_hy2 小时前
拓扑排序超详解:原理 + Kahn 贪心算法
python·算法·贪心算法
LSssT.2 小时前
【01】Python 机器学习
开发语言·python
为爱停留3 小时前
给智能体装上「刹车」:中断(Interrupts)与人工审批全解析
python
l1t3 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程39-40
开发语言·python