How to get the body from a Word DOCX document in Node.JS

Try to extract the body from a DOCX file in Node.JS can quickly become an all-day project if you’re not careful. If you would rather sidestep all the potential pitfalls that might arise from this, I might have just the method you are looking for.

For the sake of simplicity, our solution will be using an API to do the heavy lifting. This will require installation of its client, like so:

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

This library contains editDocumentDocxBody, which we will call with the following block of code, which also instances the API for us.

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.GetDocxBodyRequest(); // GetDocxBodyRequest | Document input requestvar callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.editDocumentDocxBody(reqConfig, callback);

And now we just have to input our DOCX file into the request object and send it in. A moment or two later and we will have our response. Simple.

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