1

I am trying to write a basic script to update a PowerPoint (2016) presentation, template.pptx, which contains predefined place holders for images. I am running into issues with the insert_picture() function.

As an example, the 4th slide of template.pptx contains a Picture Placeholder:

from pptx import Presentation

prs = Presentation('template.pptx')

slide4 = prs.slides[3]

for shape in slide4.placeholders:
    print('%d %s' % (shape.placeholder_format.idx, shape.name))

which gives the following output

>>> 
0 Title 3
10 Picture Placeholder 7

I proceed with

placeholder = slide4.placeholders[10] # idx key, not position
picture = placeholder.insert_picture('test.jpg')

Which throws the following error(s):

File "C:\Python273\lib\site-packages\pptx\shapes\placeholder.py", line 323, in insert_picture
pic = self._new_placeholder_pic(image_file)
 File "C:\Python273\lib\site-packages\pptx\shapes\placeholder.py", line 334, in _new_placeholder_pic
rId, desc, image_size = self._get_or_add_image(image_file)
 File "C:\Python273\lib\site-packages\pptx\shapes\placeholder.py", line 345, in _get_or_add_image
image_part, rId = self.part.get_or_add_image_part(image_file)
 File "C:\Python273\lib\site-packages\pptx\parts\slide.py", line 42, in get_or_add_image_part
image_part = self._package.get_or_add_image_part(image_file)
 File "C:\Python273\lib\site-packages\pptx\package.py", line 50, in get_or_add_image_part
return self._image_parts.get_or_add_image_part(image_file)
 File "C:\Python273\lib\site-packages\pptx\package.py", line 159, in get_or_add_image_part
image_part = self._find_by_sha1(image.sha1)
 File "C:\Python273\lib\site-packages\pptx\package.py", line 171, in _find_by_sha1
if image_part.sha1 == sha1:
AttributeError: 'Part' object has no attribute 'sha1'

I have seen this 'sha1' error mentioned in these forums but don't fully understand it (I am a bit of a novice).

Interestingly (frustratingly), I don't run into this issue when creating a PowerPoint from scratch (as per the example in the python-pptx 0.6.7 docs):

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[8])
placeholder = slide.placeholders[1]  # idx key, not position
picture = placeholder.insert_picture('test.jpg')
prs.save('new.pptx')

This runs fine, hence I am left very confused. Any help would be much appreciated. Thanks!

(Windows 7, Python 2.7.3)

1
  • have you solved? I have the same issue... Commented Jun 14, 2018 at 15:01

1 Answer 1

2

I have found a possible cause for this problem! For me the problem was that the presentation contained images that were not in either jpg, png or gif. It was a tif file. For these files, theres is no sha1 and therefore the script throws an exception. Converting/deleting these files made the problem disappear!

Please tell me if that fixes it for you too.

Sign up to request clarification or add additional context in comments.

1 Comment

I had same problem and solved after removing scalar vector graphics (SVG) images.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.