Comments (15)
thanks @scanny! I used the following to move a picture I added using add_picture to the background:
cursor_sp = shapes[0]._element
cursor_sp.addprevious(pic._element)
MS Powerpoint opens the file without complaining
from python-pptx.
An example with a picture:
picture = slide.shapes.add_picture(
image, Cm(left), Cm(top), height=Cm(height)
)
# move picture to background
slide.shapes._spTree.remove(picture._element)
slide.shapes._spTree.insert(2, picture._element) # use the number that does the appropriate job
from python-pptx.
I don't believe you have to actually remove and insert to change the order. Perhaps counterintuitive to the naming, I believe the .addprevious()
and .addnext()
lxml methods actually move the XML element in question.
So you could do something like this to move a shape from ninth position to fourth:
# shape will be positioned relative to this one, hence the name "cursor"
cursor_sp = shapes[3]._element
cursor_sp.addprevious(shapes[8]._element)
from python-pptx.
+1 for this feature request. I tried the example above but it seems to create a slide that crashes powerpoint when opened.
from python-pptx.
@scanny Any pointers as to where to look in the code for something like this ?
from python-pptx.
The z-order of shapes on a slide is determined solely by their document order in the slide part (e.g. slide1.xml). So the general gist would be to re-order that sequence of elements. The shapes in a slide are contained in the slide's "shape tree", a <p:spTree>
element with the same syntax as the group shape, just the different name. The object I expect you'll want to look at first is pptx.shapes.shapetree.SlideShapeTree and its parent BaseShapeTree, which is what you get from slide.shapes
. The _spTree
attribute on that object gives you the lxml object for the <p:spTree>
element, which would allow you to reorder shapes.
If you see how far you can get with that, feel free to ask more questions about the finer points, as needed :)
from python-pptx.
Thanks @scanny
We ended up using shapes._spTree.remove
and shapes._spTree.insert
to swap elements, like you said!
from python-pptx.
If you can post the operative bit of the code you developed, that would be a help to others who arrive here on search. Glad you got it working :)
from python-pptx.
Awesome, thanks @NotSqrt :)
from python-pptx.
when I try to do this :
shape = shapes.add_shape(MSO_SHAPE.RECTANGLE,left,top,width,height)
slide.shapes._spTree.remove(shape._element)
an error says:
shape object has no attribute ’_Element'
did I make a mistake here?
from python-pptx.
Looks like a typo. All shapes have the attribute ._element
, not ._Element
.
from python-pptx.
It is not a typo actually, I typed shape._element or shape._Element , Error always says:
shape object has no attribute '_Element'
Not a typo
from python-pptx.
What is the type of the object you get back?
shape = shapes.add_shape(MSO_SHAPE.RECTANGLE, left, top, width, height)
print(shape.__class__) # or perhaps print(shape.__class__.__name__)
And what available attributes to you find when you introspect into shape
?
print(dir(shape))
from python-pptx.
Is there a workaround other than using _spTree.remove and _spTree.insert? It creates corrupted pptx files which generate an alert when opening in MS Powerpoint. Thanks
from python-pptx.
@chrismcmc Glad to hear you got it working :)
from python-pptx.
Related Issues (20)
- Save Slide as Image HOT 2
- Local PDF to PowerPoint Converter with LLM Summarization
- Is it possible to add support for "/select,file:///..." URIs? HOT 3
- Convert PPTX slide to Image HOT 1
- How to embed an .h3d (Hyperworks output file) file? HOT 3
- Line char None values
- How to set shink in sharp when text box overflow in Python pptx HOT 1
- Fit shape to text?
- Hide a shape HOT 2
- ValueError: value must be in range 256 to 2147483647 inclusive, got 2147483648 - when adding slide HOT 13
- MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE attribute does not take effect HOT 4
- Removing video results in corrupted file. HOT 6
- Create multiple slides with concurrent.futures HOT 1
- run function on click HOT 1
- How to access and create media objects HOT 7
- copying slide-masters HOT 1
- Make xlsxwriter an optional dependency HOT 4
- There is a bug for load image under the package "langchain.document_loaders import UnstructuredImageLoader" HOT 1
- Add OLE shapes
- auto_size not working
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-pptx.