How to duplicate PowerPoint files in python? - python

Problem:
I'm trying to duplicate a slide from a presentation to another presentation, but it returns error:
Traceback (most recent call last):
File "C:\Users\vuse1\documents\visual studio 2015\Projects\PythonApplication\PythonApplication\PythonApplication.py", line 8, in
prs2.slides.add_slide(copySlide)
File "C:\Python27\lib\site-packages\pptx\slide.py", line 287, in add_slide
slide.shapes.clone_layout_placeholders(slide_layout)
File "C:\Python27\lib\site-packages\pptx\shapes\shapetree.py", line 515, in
clone_layout_placeholders for placeholder in slide_layout.iter_cloneable_placeholders():
AttributeError: 'Slide' object has no attribute 'iter_cloneable_placeholders'
Code:
from pptx import Presentation
prs1 = Presentation("C:\\Users\\vuse1\Desktop\\first.pptx")
prs2 = Presentation("C:\\Users\\vuse1\Desktop\\second.pptx")
copySlide=prs1.slides[0]
prs2.slides.add_slide(copySlide)
prs2.save("C:\\Users\\vuse1\Desktop\\second.pptx")

Copying slides isn't officially supported by the library. There's a lot of discussion about this topic. Look at this github issue for workarounds.

Related

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?
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
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.

Pyshark FileCapture doesn't work

I'm new with pyshark, and I write a sample code by searching on the tutorial
import pyshark
cap = pyshark.FileCapture("input.cap")
cap_1 = cap[0]
and then it give me an error
/Users/tingyugu/anaconda3/bin/python /Users/tingyugu/PycharmProjects/final/test.py
Traceback (most recent call last):
File "/Users/tingyugu/anaconda3/lib/python3.6/site-packages/pyshark/capture/file_capture.py", line 70, in __getitem__
next(self)
File "/Users/tingyugu/anaconda3/lib/python3.6/site-packages/pyshark/capture/file_capture.py", line 60, in __next__
packet = self._packet_generator.send(None)
StopIteration
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/tingyugu/PycharmProjects/final/test.py", line 5, in <module>
cap_1 = cap[0]
File "/Users/tingyugu/anaconda3/lib/python3.6/site-packages/pyshark/capture/file_capture.py", line 73, in __getitem__
raise KeyError('Packet of index %d does not exist in capture' % packet_index)
KeyError: 'Packet of index 0 does not exist in capture'
I know the reason is that there is no packets in the cap, but my friend can read the file by pyshark
I use the python 3.6.0 anaconda and the pyshark is 0.3.7 in anaconda
if you are on jupyter see this issue on PyShark Repo. I had the same problem, seems like pyshark does not go well with jupyter. I'm gonna assume it might have same issues with ipython as well.
there are some pull requests like this one on their repo too as a fix but nothing merged yet.

ImportError: No module named __main__ (Python 2.7 & Pickle)

So on my game I'm making, I'm trying to load the data files needed for the game, and when I load the file with pickle (The file has been loaded, I've double-checked that.) I get this error:
Traceback (most recent call last):
File "/Users/user/Downloads/Deeper-master/Deeper.py", line 257, in <module>
tutorialData = pickle.load(tutorialFile)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1384, in load
return Unpickler(file).load()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 864, in load
dispatch[key](self)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1075, in load_inst
klass = self.find_class(module, name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1130, in find_class
__import__(module)
ImportError: No module named __main__
I saw a question like this on Stack Overflow, but it's a little different than my situation, so sorry if this is a duplicate, I'm just trying to figure it out.
You can find my code here. The main script is Deeper.py, just to save time.
If you notice in your repo, you have a Deeper.pyc and Deeper.py. If I run Deeper.py, it raises the same exception as yours. If I run Deeper.pyc, it raises another exception:
Traceback (most recent call last):
File "Deeper.py", line 7, in <module>
ToolbarTile = pygame.image.load("ToolbarTile.png")
pygame.error: Couldn't open Toolbar Tile.png
So, the code (bytecode, some previous version) references ToolbarTile.png, but the exception is about Toolbar Tile.png (with a space). So I changed the file name to account for that.
Now it raises:
Traceback (most recent call last):
File "Deeper.py", line 766, in <module>
else:
File "Deeper.py", line 394, in __init__
def displayCraft(self):
pygame.error: Couldn't open options.png
There's an Options.png in your files, but it looks for options.png (lower case) (even your new code in Deeper.py looks for that). So I changed that.
Now it works, albeit with Deeper.pyc so that must be why you thought:
(The file has been loaded, I've double-checked that.)
Maybe delete the Deeper.pyc and do a git-bisect to see when the bug was introduced (ps: check your toolbar.dat, does it seem okay to you? Also check the way you're using pickle.load. Maybe you should use rb instead of r?)

epydoc AttributeError: 'Text' object has no attribute 'data'

I've not used epydoc in the last 2 years but I found it very handy to take track of my classes and methods with a very little effort.
Today I installed latest version 3.0.1 but I get this error and searching around seems no solutions are provided.
Traceback (most recent call last):-] Parsing docstrings: pyramid.reques...
File "/home/neurino/apps/env/bin/epydoc", line 13, in <module>
cli()
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/cli.py", line 965, in cli
main(options, names)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/cli.py", line 757, in main
exclude_parse=exclude_parse)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/docbuilder.py", line 275, in build_doc_index
parse_docstring(val_doc, docindex, suppress_warnings)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/docstringparser.py", line 265, in parse_docstring
api_doc.summary, api_doc.other_docs = api_doc.descr.summary()
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/markup/restructuredtext.py", line 179, in summary
try: self._document.walk(visitor)
File "/home/neurino/apps/env/lib/python2.7/site-packages/docutils/nodes.py", line 137, in walk
if child.walk(visitor):
File "/home/neurino/apps/env/lib/python2.7/site-packages/docutils/nodes.py", line 129, in walk
visitor.dispatch_visit(self)
File "/home/neurino/apps/env/lib/python2.7/site-packages/docutils/nodes.py", line 1604, in dispatch_visit
return method(node)
File "/home/neurino/apps/env/lib/python2.7/site-packages/epydoc/markup/restructuredtext.py", line 307, in visit_paragraph
m = self._SUMMARY_RE.match(child.data)
AttributeError: 'Text' object has no attribute 'data'
Is epydoc project dead?
I found a patch on epydoc tracker, it was outdated anyway this part solves the problem:
markup/restructuredtext.py
307c307,310
< m = self._SUMMARY_RE.match(child.data)
---
> try:
> m = self._SUMMARY_RE.match(child.data)
> except AttributeError:
> m = None
Epydoc has not been maintained for long time and the last release is not quite compatible with current Pyton and docutils. It is still a useful tool, though, but needs some patching.
Here are some patches I used with Epydoc to build documentation for my Python 2.7 code: http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/packages/epydoc/ (they are part of the PLD-Linux Epydoc package).
I wish someone takes over the code and continue the developmentā€¦
Objects can be tested against null (i.e. None) so that the exception could not be occurred.
if object is None:

Problems using PyQt's Resource System

I am trying to use PyQt's Resource System but it appears I have no clue what I am doing! I already have to application created, along with its GUI I am just trying to import some images to use with the program.
I used the QtDesigner to create the resource file and I compiled it using pyrcc4.exe. But when I attempt to import the resource file I get this error:
Traceback (most recent call last):
File "C:\Projects\main.py", line 14, in <module>
import main_rc
File "C:\Projects\main_rc.py", line 482, in <module>
qInitResources()
File "C:\Projects\main_rc.py", line 477, in qInitResources
QtCore.qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data)
TypeError: argument 2 of qRegisterResourceData() has an invalid type
What am I doing wrong?
pyrcc generates Python 2.x code by default.
Try regenerating your resource files using pyrcc with flag '-py3'

Categories

Resources