【问题标题】:Python pptx - part of text in cell with different colorPython pptx - 单元格中不同颜色的部分文本
【发布时间】:2019-11-26 04:53:10
【问题描述】:

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

text color in python-pptx module 找到了类似的案例,但该案例适用于“文本框架”而不是单元格。

欢迎任何意见/建议!

谢谢

【问题讨论】:

    标签: python powerpoint cell python-pptx


    【解决方案1】:

    如果您使用cell,而该示例使用text_frame(或者可能在该特定示例中使用tf),则其余代码相同。因此,要创建“带有 red 单词的句子”,其中“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

    【讨论】:

    • 是的,当我在单元格中使用 text_frame 时它正在工作。谢谢!
    猜你喜欢
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2014-06-18
    • 1970-01-01
    • 2022-10-15
    相关资源
    最近更新 更多