3

I use python 2.7 with python pptx,

I need to build a general function to center objects in my slide.

I know how to center any individual object type (textbox, table, image etc.) and need to build a function to tell which type of object is a given object and allign it properley.

I need something similar to:

if foo is bar 

condition.

I found the table general object here enter link description here and used the following code:

    if table is pptx.shapes.graphfrm.GraphicFrame.table:
        print "what"

It does not work,

How can i check if an object is some type of pptx object

Thanks!

thebeancounter
  • 4,261
  • 8
  • 61
  • 109

1 Answers1

3

The only actual objects that can appear on a slide are shapes. Visually, there might be items that "show through" from the slide master or layout, like a logo for instance, but from an object standpoint, a slide contains shapes and that's it.

So all the objects "on" a slide are in slide.shapes, and you can identify the type of each one using Shape.shape_type. This will be one of the values in the MSO_SHAPE_TYPE enumeration, of which TABLE is one.

This code will enumerate the shape types on a given slide:

for shape in slide.shapes:
    print(shape.shape_type)
scanny
  • 26,423
  • 5
  • 54
  • 80
  • so if i want to check if an object is a picture i need to use : if pic.shape_type == 13 ? where can i find a list of object types numbering? – thebeancounter Nov 14 '16 at 09:22
  • btw, this works with text box and image, but not with table with that error AttributeError: 'Table' object has no attribute 'shape_type', how can i do that with a table? – thebeancounter Nov 14 '16 at 10:07
  • @captainshai Interestingly, a table is not a shape. Rather, it is a graphical object (like a chart or smart art) contained in a GraphicsFrame shape. So you need to ask the container shape what type it is rather than the table itself. – scanny Nov 14 '16 at 19:41