How to search and replace a Text String in a Word DOCX in Node.JS
1 min readApr 12, 2020
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!