How to convert a Word DOCX Document to Text TXT in Node.JS

Microsoft Word documents certainly have their advantages when it comes to editing power. These advantages can quickly pale in comparison to the downsides, however, such as increased file size, security holes, and potential incompatibility. Let’s examine how to switch these lumbering DOCXs into simple TXTs instead.

Add this dependency reference into your package.JSON to begin installing our API client.

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

After installing, proceed by calling our conversion function from the API, 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.ConvertDocumentApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var opts = {'textFormattingMode': "textFormattingMode_example" // String | Optional; specify how whitespace should be handled when converting the document to text.  Possible values are 'preserveWhitespace' which will attempt to preserve whitespace in the document and relative positioning of text within the document, and 'minimizeWhitespace' which will not insert additional spaces into the document in most cases.  Default is 'minimizeWhitespace'.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.convertDocumentDocxToTxt(inputFile, opts, callback);

Select your whitespace preference, then input your DOCX file. Get ready to gasp in delight at your returned TXT file! Seriously, though, how easy was 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