OfficeJS Extracting all shape from every slide of a PowerPoint document

324 Views Asked by At

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?

3

There are 3 best solutions below

0
On BEST ANSWER

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.

0
On

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

0
On

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];
    }
  }
});