python - Python docx - 一行中的多种颜色
问题描述
我目前正在使用 python docx 库生成一个 word 文件。
我创建了多个包含文本和变量的表格。当行中有变量时,我将它们标记为:<Variable_name>var
我需要将分隔符 <>var 的颜色更改为红色以突出显示它。
我认为我发现的关于文本颜色的唯一方法是:
p=document.add_paragraph()
wp = p.add_run('I want this sentence colored red with fontsize=22')
wp.font.color.rgb = RGBColor(255,0,0)
但它适用于所有段落,而不是特定部分。
解决方案
字符级格式(“字体”特征)在运行级控制。运行是共享相同字符级格式的字符序列。因此,如果您想在正常格式的段落中“运行”红色字符,则需要运行三个;一前一红一后。
paragraph = document.add_paragraph()
paragraph.add_run("The part before the red bit ")
run = paragraph.add_run("the red bit")
run.font ... # --- make the font of this run red ---
paragraph.add_run(" the part after the red bit.")
推荐阅读
- java - 方法 flatMapping((
disc) -> {}, toSet()) 未为 Grouping 类型定义 - node.js - 如何缓存 Ejs 并使用 Nodejs 构建渐进式 Web 应用程序
- c# - 在没有特定运算符(+、-、/、*、%、+= %= 等)的范围内获取可整除的数字
- python - 是否有任何 Python 函数可以读取日期和月份而不是年份的 Excel 文件?
- c++ - 引导套件安装错误
- c++11 - C ++为什么用(T *)在模板中重载(T&)
- java - 升级到 Hibernate 5 和 Spring 5 后 spring-database xml 配置出现问题
- c# - 如何搜索显示名称中包含一个字母(或几个字母)的用户
- rust - Drop 不能在实现扩展特征的通用结构中使用
- google-chrome - Yubico / Webauthn 和常驻密钥