How to Replace Strings in a PowerPoint PPTX Presentation using Node.js

Cloudmersive
2 min readNov 1, 2023

--

We can easily make high-level text content edits to our PPTX files with simple API requests.

With the below code, we can take advantage of a free API that interacts with our PPTX files, replacing specified text strings with new text values. We can load our file through formData or File URL, customize our matchString (string to search for/match against) input and replaceString (string to replace matched values with) input, and use the optional matchCase parameter to determine if we want our replacement operation to be case sensitive.

To structure our API call, let’s begin by installing the SDK. We can either run this command:

npm install cloudmersive-convert-api-client --save

Or we can add this snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Finally, we can include the below code examples in our file, and we can authorize our request with a free-tier API key (these allow up to 800 API calls per month with no additional commitments):

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';



var apiInstance = new CloudmersiveConvertApiClient.TransformDocumentApi();

var matchString = "matchString_example"; // String | String to search for and match against, to be replaced

var replaceString = "replaceString_example"; // String | String to replace the matched values with

var opts = {
'inputFile': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Optional: Input file to perform the operation on.
'inputFileUrl': "inputFileUrl_example", // String | Optional: URL of a file to operate on as input. This can be a public URL, or you can also use the begin-editing API (part of EditDocumentApi) to upload a document and pass in the secure URL result from that operation as the URL here (this URL is not public).
'matchCase': true // Boolean | Optional: True if the case should be matched, false for case insensitive match. Default is false.
};

var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.transformDocumentPptxReplace(matchString, replaceString, opts, callback);

Now we can easily make quick edits to our PPTX files through our own Node.js applications.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet