Office-Script is a Microsoft Office application automation with node.js. It does not work with the Open Office XML document, instead it accesses the COM interop interface of the Offices application. Therefore, you must have Office installed! Also be carefull, Microsoft strongly recommends against Office Automation from software solutions https://support.microsoft.com/en-us/kb/257757
Only on tested with Office 2007 and Office 2016.
Work in progress.. Just ask if you have any question or feature requests!
npm install office-script --save
PowerPoint application automation.
var path = require('path');
var powerpoint = require('office-script').powerpoint;
//Create a new instance of PowerPoint and try to open the presentation
powerpoint.open(path.join(__dirname, 'Presentation01.pptx'), function(err, presentation) {
if(err) throw err;
//use presentation object
console.log('Presentation path:', presentation.attr({name:'Path'}, true));
//Get slides
presentation.slides(null, function(err, slides) {
if(err) throw err;
console.log('Slides count:', slides.length);
//get shapes on slide 1
slides[0].shapes(null, function(err, shapes) {
console.log('Shape count on slide 1:', shapes.length);
shapes[0].attr({'name':'Text', 'value': 'Fu Bar'}, true); //Set text value
console.log('Get text first shape:', shapes[0].attr({'name':'Text'}, true));
//close presentation
presentation.close(null, function(err) {
if(err) throw err;
//exit powerpoint
powerpoint.quit()
});
});
});
});
Office-Script is written in an async pattern, but application automation can has serious problems with async...
Because of this, I recommend to use the sync presentation wrapper. It also has the more readable API.
var path = require('path')
var Presentation = require('office-script').Presentation
var presentation
try {
// open a new PPT Presentation
presentation = new Presentation(path.join(__dirname, 'Presentation01.pptx'))
// get presentation slides
var slides = presentation.slides()
console.log('Slide count: ', slides.length)
// Get all shapes of the first slide
var shapes = slides[0].shapes()
console.log('Title shape count:', shapes.length)
// get name and text of the first shape
console.log('shape name:', shapes[0].name())
console.log('Title shape text:', shapes[0].text())
// change name of the first
shapes[0].name('First Shape')
// change text of the first
shapes[0].text('FuBar')
// Setter retun the destination object so you can chain them
shapes[0].top(10).left(10).height(100).width(100)
// Save presentation as PDF (sync)
presentation.saveAs({name: path.join(__dirname, 'Presentation01.pdf'), type: 'pdf'})
// SaveAs new presentation and quit application
presentation.saveAs(path.join(__dirname, 'Presentation01_New.pptx'))
} catch (e) {
console.error(e)
}
if (presentation) {
presentation.quit() // Close presentation & quit application
}
If path exists the presentation will be open.
If path
does not exist, an allready open presentation with the name of path
will be used.
If path
is missing or null
, the active presentation is used.
Adds a new empty slide on the provided postiton an returns it. If no postiton was provided, the new slide will be added at the end.
Closes the presentation without exiting powerpoint itself.
Closes the presentation and powerpoint itself.
Saves the presentation.
Saves the presentation to the provided path and name.
Saves the presentation as copy to the provided path and name.
Find and replace text in the entire presentation.
Without parameters, returns all builtin properties with their vlaues.
With property
, returns value of the specific builtin property.
With property
and value
, sets value of specific builtin property.
Without parameters, returns all custom properties with their vlaues.
With property
, returns value of the specific custom property.
With property
and value
, sets/ccustomreates value of specific custom property
Get presentation slides. Optional filterd by the selector.
Get active slide.
Pastes the slides on the Clipboard into the Slides collection for the presentation. Returns the pasted slide. Index -1
moves the slide to the end of the presentation.
presentation01.slides()[0].copy() // Copy first slide from presentation presentation01
presentation02.pasteSlide() // Paste it on the end in presentation presentation02
If value
is provided, it will set the property and return the slide
Get presentation shapes. Optional filterd by the selector. Context is an optional slides array.
Get selected shape.
Get slide shapes. Optional filterd by the selector.
// export chart-shapes as EMF
if (shapes[i].has('chart')) {
shapes[i].exportAs({path: path.join(__dirname, shapes[i].name() + '.emf'), type: 'emf'})
}
If value
is provided, it will set the property and return the shape. If not, it will return the value.
Get paragraph object. Optional filterd by start and length.
If value
is provided, it will set the property and return the shape. If not, it will return the value.
Delete paragraph