How to split a single Word DOCX into Separate Documents by Page in Node.JS

Having the option to separate DOCX pages into separate files can certainly be useful, but setting this up can really eat up your afternoon. I propose a different solution, one that will instead require but a few minutes. Let’s look at how it works.

We are going to use a Cloudmersive API client as our shortcut, which will require installation:

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

Our next step is to instantiate an API using a free key. Then proceed to use this instance for a splitDocumentDocx function call.

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.SplitDocumentApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var opts = {'returnDocumentContents': true // Boolean | Set to true to return the contents of each Worksheet directly, set to false to only return URLs to each resulting document.  Default is true.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.splitDocumentDocx(inputFile, opts, callback);

Easy! Your return will include all of the individual page files. Alternatively you can set returnDocumentContents to false, which will give you download URLs instead.

Image for post
Image for post
Why get a headache when an API can do it for you?

Written by

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