Search code examples
pythonformatpowerpointpython-pptxtextcolor

how to change colour of specific words in a powerpoint in python


I am using python-pptx library of python to automate a powerpoint presentation that we are required to create on monthly basis as part of a report. Certain words in text of existing presentation are coloured red, green or yellow. I'd like to implement this using python-pptx library or any other method available but so far I don't see any way that I can use to achieve this. Please find below screenshot for reference and thanks in advance for help.

individual Words coloured differently


Solution

  • You would do this:

    from pptx.dml.color import RGBColor
    font.color.rgb = RGBColor(0x80, 0x18, 0x18)
    

    Furthermore, if you are coloring multiple words/sentences:

    from pptx.util import Pt
    
    p = tf.add_paragraph()
    run = p.add_run()
    blue = p.add_run()
    blue.text = 'this is blue'
    blue.font.color.rgb = RGBColor(0, 0, 255)
    run.text = 'this is red'
    run.font.color.rgb = RGBColor(178,34,34)
    

    Python-pptx docs: link

    RGB colors: link