使用 python-pptx 高亮显示 ppt 中的单词

Highlight the word in ppt using python-pptx

我想阅读ppt并突出显示幻灯片中的某些单词(如果背景颜色不可用则添加背景颜色然后更改字体颜色)。我可以在句子级别做到这一点,即如果该词包含在段落中,那么它将改变整个文本的字体颜色,但我只想改变该词的颜色。

下面的突出显示怎么样?

def set_highlight(run, color):
    # get run properties
    rPr = run._r.get_or_add_rPr()

    # Create highlight element
    hl = OxmlElement("a:highlight")

    # Create specify RGB Colour element with color specified
    srgbClr = OxmlElement("a:srgbClr")
    setattr(srgbClr, "val", color)

    # Add colour specification to highlight element
    hl.append(srgbClr)

    # Add highlight element to run properties
    rPr.append(hl)

    return run

这是对 XML 的破解,但要突出显示(文本背景),这是您必须要做的。