Marci
Marci

Reputation: 137

How can i work with templates in Python-pptx

I know this module is not very popular but if you know the answer then please help me out with it.

My code is:

from pptx import Presentation
prs = Presentation('template.pptx')
title_slide_layout = prs.slide_layout[0]
# print(len(prs.slide_layout))
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Python 3.6 - Turtle Race"
subtitle.text = "Data Analytics&Visualization with random generated data"

prs.save("out.pptx")

An error I have got:

Traceback (most recent call last):
  File "D:/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton/turtleRace/presentationMaker.py", line 8, in <module>
    prs = Presentation('template.pptx')
  File "D:\!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton\turtleRace\venv\lib\site-packages\pptx\api.py", line 28, in Presentation
    presentation_part = Package.open(pptx).main_document_part
  File "D:\!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton\turtleRace\venv\lib\site-packages\pptx\opc\package.py", line 103, in main_document_part
    return self.part_related_by(RT.OFFICE_DOCUMENT)
  File "D:\!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton\turtleRace\venv\lib\site-packages\pptx\opc\package.py", line 136, in part_related_by
    return self.rels.part_with_reltype(reltype)
  File "D:\!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton\turtleRace\venv\lib\site-packages\pptx\opc\package.py", line 439, in part_with_reltype
    rel = self._get_rel_of_type(reltype)
  File "D:\!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton\turtleRace\venv\lib\site-packages\pptx\opc\package.py", line 491, in _get_rel_of_type
    raise KeyError(tmpl % reltype)
KeyError: "no relationship of type 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' in collection"

A picture of my project interpreter: PICTURE

So why I have got this error?

Upvotes: 0

Views: 2348

Answers (2)

Marci
Marci

Reputation: 137

I have found the solution!!!

Before I saved the file(called template) as Strict Open XML Presentation(.pptx) and not as PowerPoint Presentation(.pptx)

It's now opening the file but now I have another error:

Traceback (most recent call last):
  File "D:/!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!_!Piton/turtleRace/presentationMaker.py", line 9, in <module>
    title_slide_layout = prs.slide_layout[0]
AttributeError: 'Presentation' object has no attribute 'slide_layout'

Everything is the same just the saving method in PowerPoint has changed.

Upvotes: 1

Sai Kiran Sangam
Sai Kiran Sangam

Reputation: 368

It is an issue about the type when you save the file as Strict Open XML Presentation. Try the standard Presentation document.

You can get more informations about relations inside the file using opc-diag:

You can resolve error Here

Trying to fix a old file:

Extract

unzip <FILE> -d old-file

Repackage it into a new fresh file

opc repackage bad-file new-file.docx

diff of relationships

opc diff-item test.docx test-ok.docx .rels

Upvotes: 1

Related Questions