n8n-nodes-mammoth
Version:
n8n node to extract text from DOCX using mammoth
53 lines (44 loc) • 1.49 kB
JavaScript
const mammoth = require("mammoth");
module.exports = {
nodes: [
{
name: "MammothDocxExtract",
displayName: "DOCX Extract",
description: "Extract text from a DOCX file using mammoth",
group: ["transform"],
version: 1,
inputs: ["main"],
outputs: ["main"],
properties: [
{
displayName: "Binary Property",
name: "binaryPropertyName",
type: "string",
default: "data",
description: "Name of the binary property which contains the DOCX file",
},
],
async execute() {
const items = this.getInputData();
const returnData = [];
for (let i = 0; i < items.length; i++) {
const item = items[i];
if (!item.binary) {
throw new Error("No binary data found on input item.");
}
const binaryPropertyName = this.getNodeParameter("binaryPropertyName", i);
const binaryData = item.binary[binaryPropertyName];
if (!binaryData) {
throw new Error(Binary property "${binaryPropertyName}" does not exist.);
}
const buffer = Buffer.from(binaryData.data, "base64");
const result = await mammoth.extractRawText({ buffer });
returnData.push({
json: { text: result.value },
});
}
return this.prepareOutputData(returnData);
},
},
],
};