2

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")
Shreamy
  • 341
  • 2
  • 16

1 Answers1

1

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'

scanny
  • 26,423
  • 5
  • 54
  • 80
  • Actually, when I read the docs, the slide mention that slide layout is a must to operate on the slides ? is it true? – Shreamy Dec 11 '19 at 06:37
  • A slide-layout is required to _add_ a slide. The slide layout is not required to _edit_ a slide though. The slide already knows which layout it was created from. – scanny Dec 12 '19 at 17:43