Paper Office
paper-pptxAPI referenceshapes.shapetree

SlideShapes

paper-pptx 0.2.0 API reference

Sequence of shapes appearing on a slide.

The first shape in the sequence is the backmost in z-order and the last shape is topmost. Supports indexed access, len(), index(), and iteration.

Attributes

element

attributeelement

The lxml element proxied by this object.

parent

attributeparentSlide

part

attributepartSlidePart

placeholders

attributeplaceholdersSlidePlaceholders

Sequence of placeholder shapes in this slide.

title

attributetitleShape | None

The title placeholder shape on the slide.

None if the slide has no title placeholder.

turbo_add_enabled

attributeturbo_add_enabledbool

True if "turbo-add" mode is enabled. Read/Write.

EXPERIMENTAL: This feature can radically improve performance when adding large numbers (hundreds of shapes) to a slide. It works by caching the last shape ID used and incrementing that value to assign the next shape id. This avoids repeatedly searching all shape ids in the slide each time a new ID is required.

Performance is not noticeably improved for a slide with a relatively small number of shapes, but because the search time rises with the square of the shape count, this option can be useful for optimizing generation of a slide composed of many shapes.

Shape-id collisions can occur (causing a repair error on load) if more than one Slide object is used to interact with the same slide in the presentation. Note that the Slides collection creates a new Slide object each time a slide is accessed (e.g. slide = prs.slides[0], so you must be careful to limit use to a single Slide object.

Functions

__getitem__

func__getitem__(idx: int) -> BaseShape

Return shape at idx in sequence, e.g. shapes[2].

paramidxint

Returns

pptx.shapes.base.BaseShape

__iter__

func__iter__() -> Iterator[BaseShape]

Generate a reference to each shape in the collection, in sequence.

Returns

typing.Iterator[pptx.shapes.base.BaseShape]

__len__

func__len__() -> int

Return count of shapes in this shape tree.

A group shape contributes 1 to the total, without regard to the number of shapes contained in the group.

Returns

int

add_chart

funcadd_chart(chart_type: XL_CHART_TYPE, x: Length, y: Length, cx: Length, cy: Length, chart_data: ChartData) -> Chart

Add a new chart of chart_type to the slide.

The chart is positioned at (x, y), has size (cx, cy), and depicts chart_data. chart_type is one of the :ref:XlChartType enumeration values. chart_data is a ChartData object populated with the categories and series values for the chart.

Note that a GraphicFrame shape object is returned, not the Chart object contained in that graphic frame shape. The chart object may be accessed using the :attr:chart property of the returned GraphicFrame object.

paramchart_typeXL_CHART_TYPE
paramxLength
paramyLength
paramcxLength
paramcyLength
paramchart_dataChartData

Returns

pptx.chart.chart.Chart

add_connector

funcadd_connector(connector_type: MSO_CONNECTOR_TYPE, begin_x: Length, begin_y: Length, end_x: Length, end_y: Length) -> Connector

Add a newly created connector shape to the end of this shape tree.

connector_type is a member of the :ref:MsoConnectorType enumeration and the end-point values are specified as EMU values. The returned connector is of type connector_type and has begin and end points as specified.

paramconnector_typeMSO_CONNECTOR_TYPE
parambegin_xLength
parambegin_yLength
paramend_xLength
paramend_yLength

Returns

pptx.shapes.connector.Connector

add_copy

funcadd_copy(shape)

Return a copy of shape (from this or another slide) added to this slide.

paper-pptx addition. The copy gets fresh shape ids; its relationships follow the pinned policy: image/media parts shared, external (hyperlink) relationships copied, charts deep-copied WITH their embedded workbooks and style parts. Any other relationship type refuses with RelationshipPolicyError before anything changes. A shape from another presentation raises TargetNotFoundError.

paramshape

add_group_shape

funcadd_group_shape(shapes: Iterable[BaseShape] = ()) -> GroupShape

Return a GroupShape object newly appended to this shape tree.

The group shape is empty and must be populated with shapes using methods on its shape tree, available on its .shapes property. The position and extents of the group shape are determined by the shapes it contains; its position and extents are recalculated each time a shape is added to it.

paramshapesIterable[BaseShape]
= ()

Returns

pptx.shapes.group.GroupShape

add_movie

funcadd_movie(movie_file: str | IO[bytes], left: Length, top: Length, width: Length, height: Length, poster_frame_image: str | IO[bytes] | None = None, mime_type: str = CT.VIDEO) -> GraphicFrame

Return newly added movie shape displaying video in movie_file.

EXPERIMENTAL. This method has important limitations:

  • The size must be specified; no auto-scaling such as that provided by :meth:add_picture is performed.
  • The MIME type of the video file should be specified, e.g. 'video/mp4'. The provided video file is not interrogated for its type. The MIME type video/unknown is used by default (and works fine in tests as of this writing).
  • A poster frame image must be provided, it cannot be automatically extracted from the video file. If no poster frame is provided, the default "media loudspeaker" image will be used.

Return a newly added movie shape to the slide, positioned at (left, top), having size (width, height), and containing movie_file. Before the video is started, poster_frame_image is displayed as a placeholder for the video.

parammovie_filestr | IO[bytes]
paramleftLength
paramtopLength
paramwidthLength
paramheightLength
paramposter_frame_imagestr | IO[bytes] | None
= None
parammime_typestr
= CT.VIDEO

Returns

pptx.shapes.graphfrm.GraphicFrame

add_ole_object

funcadd_ole_object(object_file: str | IO[bytes], prog_id: str, left: Length, top: Length, width: Length | None = None, height: Length | None = None, icon_file: str | IO[bytes] | None = None, icon_width: Length | None = None, icon_height: Length | None = None) -> GraphicFrame

Return newly-created GraphicFrame shape embedding object_file.

The returned graphic-frame shape contains object_file as an embedded OLE object. It is displayed as an icon at left, top with size width, height. width and height may be omitted when prog_id is a member of PROG_ID, in which case the default icon size is used. This is advised for best appearance where applicable because it avoids an icon with a "stretched" appearance.

object_file may either be a str path to a file or file-like object (such as io.BytesIO) containing the bytes of the object to be embedded (such as an Excel file).

prog_id can be either a member of pptx.enum.shapes.PROG_ID or a str value like "Adobe.Exchange.7" determined by inspecting the XML generated by PowerPoint for an object of the desired type.

icon_file may either be a str path to an image file or a file-like object containing the image. The image provided will be displayed in lieu of the OLE object; double-clicking on the image opens the object (subject to operating-system limitations). The image file can be any supported image file. Those produced by PowerPoint itself are generally EMF and can be harvested from a PPTX package that embeds such an object. PNG and JPG also work fine.

icon_width and icon_height are Length values (e.g. Emu() or Inches()) that describe the size of the icon image within the shape. These should be omitted unless a custom icon_file is provided. The dimensions must be discovered by inspecting the XML. Automatic resizing of the OLE-object shape can occur when the icon is double-clicked if these values are not as set by PowerPoint. This behavior may only manifest in the Windows version of PowerPoint.

paramobject_filestr | IO[bytes]
paramprog_idstr
paramleftLength
paramtopLength
paramwidthLength | None
= None
paramheightLength | None
= None
paramicon_filestr | IO[bytes] | None
= None
paramicon_widthLength | None
= None
paramicon_heightLength | None
= None

Returns

pptx.shapes.graphfrm.GraphicFrame

add_picture

funcadd_picture(image_file: str | IO[bytes], left: Length, top: Length, width: Length | None = None, height: Length | None = None) -> Picture

Add picture shape displaying image in image_file.

image_file can be either a path to a file (a string) or a file-like object. The picture is positioned with its top-left corner at (top, left). If width and height are both None, the native size of the image is used. If only one of width or height is used, the unspecified dimension is calculated to preserve the aspect ratio of the image. If both are specified, the picture is stretched to fit, without regard to its native aspect ratio.

paramimage_filestr | IO[bytes]
paramleftLength
paramtopLength
paramwidthLength | None
= None
paramheightLength | None
= None

Returns

pptx.shapes.picture.Picture

add_shape

funcadd_shape(autoshape_type_id: MSO_SHAPE, left: Length, top: Length, width: Length, height: Length) -> Shape

Return new Shape object appended to this shape tree.

autoshape_type_id is a member of :ref:MsoAutoShapeType e.g. MSO_SHAPE.RECTANGLE specifying the type of shape to be added. The remaining arguments specify the new shape's position and size.

paramautoshape_type_idMSO_SHAPE
paramleftLength
paramtopLength
paramwidthLength
paramheightLength

Returns

pptx.shapes.autoshape.Shape

add_table

funcadd_table(rows: int, cols: int, left: Length, top: Length, width: Length, height: Length) -> GraphicFrame

Add a GraphicFrame object containing a table.

The table has the specified number of rows and cols and the specified position and size. width is evenly distributed between the columns of the new table. Likewise, height is evenly distributed between the rows. Note that the .table property on the returned GraphicFrame shape must be used to access the enclosed Table object.

paramrowsint
paramcolsint
paramleftLength
paramtopLength
paramwidthLength
paramheightLength

Returns

pptx.shapes.graphfrm.GraphicFrame

add_textbox

funcadd_textbox(left: Length, top: Length, width: Length, height: Length) -> Shape

Return newly added text box shape appended to this shape tree.

The text box is of the specified size, located at the specified position on the slide.

paramleftLength
paramtopLength
paramwidthLength
paramheightLength

Returns

pptx.shapes.autoshape.Shape

build_freeform

funcbuild_freeform(start_x: float = 0, start_y: float = 0, scale: tuple[float, float] | float = 1.0) -> FreeformBuilder

Return FreeformBuilder object to specify a freeform shape.

The optional start_x and start_y arguments specify the starting pen position in local coordinates. They will be rounded to the nearest integer before use and each default to zero.

The optional scale argument specifies the size of local coordinates proportional to slide coordinates (EMU). If the vertical scale is different than the horizontal scale (local coordinate units are "rectangular"), a pair of numeric values can be provided as the scale argument, e.g. scale=(1.0, 2.0). In this case the first number is interpreted as the horizontal (X) scale and the second as the vertical (Y) scale.

A convenient method for calculating scale is to divide a Length object by an equivalent count of local coordinate units, e.g. scale = Inches(1)/1000 for 1000 local units per inch.

paramstart_xfloat
= 0
paramstart_yfloat
= 0
paramscaletuple[float, float] | float
= 1.0

Returns

pptx.shapes.freeform.FreeformBuilder

chart_by_name

funcchart_by_name(name: str)

Return the Chart held by the shape on this slide named name.

paper-pptx addition, the chart-addressing half of safe chart-data replacement. Group-aware: shapes inside groups are found too. Raises TargetNotFoundError when no shape has that name, or when shapes with the name exist but none holds a chart (the message says what was found instead). Raises AmbiguousTargetError when more than one chart-bearing shape has the name — this API never guesses between them.

paramnamestr

clone_layout_placeholders

funcclone_layout_placeholders(slide_layout: SlideLayout) -> None

Add placeholder shapes based on those in slide_layout.

Z-order of placeholders is preserved. Latent placeholders (date, slide number, and footer) are not cloned.

paramslide_layoutSlideLayout

Returns

None

clone_placeholder

funcclone_placeholder(placeholder: LayoutPlaceholder) -> None

Add a new placeholder shape based on placeholder.

paramplaceholderLayoutPlaceholder

Returns

None

delete

funcdelete(shape) -> None

Remove shape from this slide, with relationship hygiene.

paper-pptx addition. Relationships referenced by the removed subtree are dropped unless something else in the part still references them (two pictures can share one image relationship). A shape that is not a direct member of this collection — including a shape inside a group — raises TargetNotFoundError (delete the group, or ungroup first).

paramshape

Returns

None

index

funcindex(shape: BaseShape) -> int

Return the index of shape in this sequence.

Raises ValueError if shape is not in the collection.

paramshapeBaseShape

Returns

int

move

funcmove(shape, to_index: int) -> None

Move shape to 0-based to_index in this collection's z-order.

paper-pptx addition. Index 0 is backmost, the last index topmost — the same order this collection iterates. to_index outside range raises ValueError; a shape not directly in this collection raises TargetNotFoundError.

paramshape
paramto_indexint

Returns

None

ph_basename

funcph_basename(ph_type: PP_PLACEHOLDER) -> str

Return the base name for a placeholder of ph_type in this shape collection.

There is some variance between slide types, for example a notes slide uses a different name for the body placeholder, so this method can be overriden by subclasses.

paramph_typePP_PLACEHOLDER

Returns

str

picture_by_name

funcpicture_by_name(name: str)

Return the Picture on this slide named name (group-aware).

paper-pptx addition, with the same contract as chart_by_name: TargetNotFoundError when nothing (or nothing picture-shaped) has the name, AmbiguousTargetError when several pictures do.

paramnamestr

shape_by_name

funcshape_by_name(name: str)

Return the single shape on this slide named name (group-aware).

paper-pptx addition: TargetNotFoundError / AmbiguousTargetError, never first-match.

paramnamestr

table_by_name

functable_by_name(name: str)

Return the Table held by the graphic frame on this slide named name.

paper-pptx addition, same contract as chart_by_name.

paramnamestr

On this page