Powerpoint: Copying individual slides into new pow

2019-09-01 01:08发布

I want to save each slide in a powerpoint-file into seperate powerpoint-files, as well as store png images of each slide. In the end I want to create an archive of individual unique slides, so that all slides that are shown on some info screens are stored in this archive. This is what I have to far:

function exportSingleSlides(pathname, filename){
    var app = new ActiveXObject("PowerPoint.Application");
    app.Visible = true;

    // Open the presentation
    var presentation = app.Presentations.Open(pathname+filename, true);
    var tmpPresentation; // Used to store a new presentation for each slide.

    var e = new Enumerator(presentation.Slides);
    var slide;
    e.moveFirst();
    var i = 0;
    while (!e.atEnd()) {
        i++;
        slide = e.item(); // gets this slide

        // Export slide to png
        slide.Export(pathname + 'slide' + (i < 10 ? '0' + i : i) + '.png', 'PNG', 1920, 1080);

        // Open new presentation
        tmpPresentation = app.Presentations.Add();

        // Copy current slide and paste into new presentation
        slide.Copy();
        tmpPresentation.Slides.Paste(1);

        tmpPresentation.SaveAs(pathname + 'slide' + (i < 10 ? '0' + i : i));
        tmpPresentation.Close();
        e.moveNext();
    }

    // Close the presentation. (But how do I close powerpoint entirely?)
    presentation.Close();
    app.Quit();
}

// Which file are we looking at?
var pathname = 'C:\\Users\\Stian\\Dropbox\\jobb\\RB\\powerpoint parser\\';
var filename = 'test.pptx';

exportSingleSlides(pathname, filename);

This saves the slides to png as I want, and it also saves each slide as it's own powerpoint-file. But it's not copying it as it should. I think it might be missing the template, because headers and footers aren't included in the new copy. I'm using the powerpoint developer reference pages, but I can't seem to find a good way to copy/duplicate a slide as-is. Any suggestions?

1条回答
ら.Afraid
2楼-- · 2019-09-01 01:36

I've figured it out. Doing as below instead of just copying will do the trick.

// Get the layout from the source slide
layout = slide.CustomLayout;

// Copy current slide and paste into new presentation
slide.Copy();
tmpPresentation.Slides.Paste(1);

// Set the layout
tmpPresentation.Slides(1).CustomLayout = layout;

Now I'm getting an unspecified error when trying to save the new file, but that's a whole other problem.

查看更多
登录 后发表回答