Shreamy
Shreamy

Reputation: 361

How to edit slides based on slide_id Python pptx

Problem I have been looking for a solution but can't find enough documentation to figure it out. I am trying to edit a slide that I had copied into a directory. (I have make sure to clean out all the data only leaving behind some logo) What I did is I have printed out the slide ID and tried to access the slide by their ID and hopefully add some textbox for starters. However, I kept getting

File "masterScript_2.2.py", line 7385, in <module>
title.text = "Hello, World!"
AttributeError: 'NoneType' object has no attribute 'text' 

Question Is there any solution for it? Thanks and greatly appreciated.

Code

pptxDst = os.getcwd() + "/" + "test.pptx"
# Load the powerpoint in the respective month folder
prs = Presentation(pptxDst)


for slide in prs.slides:
    print(slide.slide_id)

# Get the slide by id to access it
slide_1 = prs.slides.get(256)
print(slide_1)
title = slide_1.shapes.title

title.text = "Hello, World!"
prs.save("test.pptx")

Upvotes: 2

Views: 2547

Answers (1)

scanny
scanny

Reputation: 28883

It appears you have accessed the slide just fine but the slide does not have a title placeholder.

Shapes.title returns the title placeholder if there is one, or None if the slide has no title. If you removed all shapes except for a logo, there wouldn't be a title placeholder to find.

This would make the offending line equivalent to:

None.text = "Hello, World!"

Which explains your error message AttributeError: 'NoneType' object has no attribute 'text'

Upvotes: 1

Related Questions