How to search and replace a Text String in a Word DOCX in Node.JS

By the end of today’s tutorial, you will have implemented a search and replace function for simple text strings in DOCX files. We can get this done very easily in Node.JS through use of a lovely little API designed for the task. Let’s dive into the process.

Client installation comes first:

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

After running that command and waiting for the installation to finish, move on to calling the following function, like so:

var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveConvertApiClient.EditDocumentApi();var reqConfig = new CloudmersiveConvertApiClient.ReplaceStringRequest(); // ReplaceStringRequest | Document string replacement configuration inputvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editDocumentDocxReplace(reqConfig, callback);

Now enter in the details for your search and replace, and give it a test drive. It’s as easy as that!

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store