Python pptx-单元格中具有不同颜色的文本部分

问题描述 投票:1回答:1

我正在使用pptx模块生成带表的幻灯片。我可以更改每个单元格中的字体,但我还需要更改文本中特定单词的字体。在示例“生成random句子作为示例”中。在这个世界上,“随机”是大胆的。

text color in python-pptx module发现了类似的情况,但该情况适用于“文本框架”而不适用于单元格。

欢迎任何建议/建议!

谢谢

python powerpoint cell python-pptx
1个回答
0
投票

如果在该示例使用cell的情况下使用text_frame(或者在该特定示例中使用tf的情况),其余代码相同。因此,要创建“带有red单词的句子”,其中“红色”以红色出现:

from docx.shared import RGBColor

# ---reuse existing default single paragraph in cell---
paragraph = cell.paragraphs[0]

# ---add distinct runs of text one after the other to
# --- form paragraph contents
paragraph.add_run("A sentence with a ")

# ---colorize the run with "red" in it---
red_run = paragraph.add_run("red")
red_run.font.color.rgb = RGBColor(255, 0, 0)

paragraph.add_run(" word.")

您可以在此处的文档中找到其他详细信息:https://python-docx.readthedocs.io/en/latest/user/text.html#font-color

© www.soinside.com 2019 - 2024. All rights reserved.