1

We want to extract all shape of an opened PowerPoint document from an add-in.

We haven't found much documentation on how to use the API for PowerPoint specifically (this is all we found). We did able to get the selected shape data from the slide with .getSelectedShapes() but we need get shapes from entire presentation.

How might we best approach this?

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • OfficeJS is nowhere as well-developed for PowerPoint as it is for, say, Excel. But perhaps explain what you mean by "get the shapes" or extract them. Extract them to what, exactly.? – Steve Rindsberg Mar 02 '23 at 19:16

3 Answers3

1

You can use the following sequence of calls to get all shapes for a slide:

const shapes = context.presentation.slides.getItemAt(0).shapes;

For example, the following code shows how to delete shapes:

await PowerPoint.run(async (context) => {
    // Delete all shapes from the first slide.
    const sheet = context.presentation.slides.getItemAt(0);
    const shapes = sheet.shapes;

    // Load all the shapes in the collection without loading their properties.
    shapes.load("items/$none");
    await context.sync();
        
    shapes.items.forEach(function (shape) {
        shape.delete();
    });
    await context.sync();
});

See Work with shapes using the PowerPoint JavaScript API for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
1

The following code shows how to get all shapes.

await PowerPoint.run(async (context) => {
  context.presentation.load("slides");
  await context.sync();

  const slideCount = context.presentation.slides.getCount()
  await context.sync();

  for (let index = 0; index < slideCount.value; index++) {
    const slide = context.presentation.slides.getItemAt(index);
    slide.load("shapes");
    await context.sync();

    for (let j = 0; j < slide.shapes.items.length; j++) {
      const shape = slide.shapes.items[j];
    }
  }
});
Daniel
  • 11
  • 1
0

Get the slides from the presentation using presentation.slides, then get the shapes from each slide using slide.shapes.

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45