python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Add Bullet list on existing slides doesn't works

Open FPRM opened this issue 2 years ago • 2 comments

Python == 3.7 python-pptx==0.6.21

I try to create a simple bullet list in an existing presentation template on an existing slide. by Following the exemple on the read the doc page everything works but the example requires to create a new slide. i want to add my bullet list on as specific space in a specific place, and it doesn't work.

My code is as following

from pptx import Presentation
from pptx.util import Cm , Pt
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN


# list of bulleted element
paragraph_strs = [
    'test item1',
    'test item2',
    'test item3'
]
# Open Templatetes and position on slide 2
prs = Presentation('emptytest.pptx')
docslide = prs.slides[1]

# create text box according to template needed
txBox = docslide.shapes.add_textbox(Cm(2), Cm(2), Cm(17.15), Cm(5))
tf = txBox.text_frame
# iterate bullet list and add text as paragraph
for para_str in paragraph_strs:
    p = tf.add_paragraph()
    p.text = para_str
    p.alignment = PP_ALIGN.LEFT
    font = p.font
    font.name = 'Ubuntu'
    font.size = Pt(12)
    p.font.color.rgb = RGBColor(140, 140, 140)
    p.level = 1

prs.save('testoutput.pptx')

And the result is https://i.imgur.com/IfgmfHz.png as my desired output should look like. https://i.imgur.com/Hi5bGpm.png And if i may ask a nice to have question, would be to have bullets formated in dedicated color like https://imgur.com/UrrsBu8

Thanks a lot

FPRM avatar Apr 13 '22 08:04 FPRM

I am wondering about the same thing as @FPRM Did not find anything in the documentation about this, but I am sure there is a way. Can anyone help?

cltj avatar Aug 25 '22 05:08 cltj

I'm wondering it is related to prs.slides[1] not using a slide layout (slide master slide) that contains a placeholder text box. If the placeholder text box is fomratted to use bullet points, then that might be the way to go.

To see what I mean, have a look at https://python-pptx.readthedocs.io/en/latest/user/quickstart.html#bullet-slide-example

If you undertake that that example, then open the created test.pptx file:

  • Select the slide, then have a look at it's implemented slide master slide - you'll see the text box placeholder is formatted to use bullets at every level.

Not sure if that helps (or if you even care anymore 18 months on), but thought I'd share in case it does :)

NovoLearn avatar Apr 03 '24 04:04 NovoLearn