简体   繁体   中英

How can I change the font color inside shape with python-pptx?

I am trying to create a few shapes using python-pptx module in Python.

I have got the shapes created however I am unable to edit the font color for the text inside shape. By default it displays in white color. I am trying to see how could I change the text color. Given below is what I have built thus far:

left = Inches(1.0)
top = Inches(3.9)
width = Inches(1.2)
height = Inches(2.5)
shape = shapes.add_shape(
MSO_SHAPE.RECTANGLE, left, top, width, height
)
fill = shape.fill
fill.solid()
fill.fore_color.rgb = RGBColor(226, 206, 72)  # This RGB code is the background color that I want the shape to be in 
fill.fore_color.brightness = -0.3
text_frame = shape.text_frame
text_frame.clear()
p = text_frame.paragraphs[0]
run = p.add_run()
run.text = 'Test message'
font = run.font
font.name = 'Arial'
font.size = Pt(16)
font.bold = True

Looking at the documentation , you should be able to do this by adding one line to your code (at the bottom):

font.color.rgb = RGBColor(0x00, 0x00, 0x00)        # this would be black

Note: remember, to use RGBColor, you would have to import it:

from pptx.dml.color import RGBColor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM