Word
This documents the Word module and how to use it from JavaScript flows.
You can load the module using the following code:
var c = Module.load("Word", {version: "X.Y.Z"});
Open and inspect a document
To parse and inspect a document, use the open
method:
var doc = c.open("path/to/document.docx");
If you need to edit the document, you can use the open
method with the editable: true
option:
var doc = c.open("path/to/document.docx", {editable: true});
Remember to close()
all documents you’ve opened
If you don’t you wont be able to access the document until Manatee is restarted.
The returned doc
is a document-object-model of a sorts of the document in question. You can use it to retrieve all paragraphs, tables, lists, etc. in the document.
Each node in the inspect()
output has a type
property that indicates the type of node. The following types are supported:
document
: the root node of the documentparagraph
: a paragraphtable
: a tablelist
: a listlistitem
: an item in a listtext
: a text node
In addition you can invoke the toHtml()
method on each node to get a simplified HTML representation of the node.
Closing a document
When you are done with a document, you must close it using the close
method:
doc.close();
Always close all documents opened.
Edit a document
You can replace text in a document using the replace
method:
doc.replace("find", "replace");
Remember to open the document with the editable: true
option
If you don’t, the replace
method will throw an error.
var doc = c.open("path/to/document.docx", { editable: true });
You can also replace text using a regular expression:
doc.replace(/find/g, "replace");
or use a function to replace text:
doc.replace(/find/g, function (match) {
return match.toUpperCase();
});
You can also supply a map of values to be replaced:
doc.replace({
"find1": "replace1",
"find2": "replace2"
});
WARNING
The replace
will modify the file in-place so if you need the original file, make a copy before opening it:
Fs.cp("path/to/document.docx", "path/to/document.docx.bak");
var doc = c.open("path/to/document.docx", { editable: true });
...
In addition to the replace
function you can also modify text more directly.
Each text
node has a text
property that you can use to get or set the text of the node:
var doc = c.open("path/to/document.docx");
var t = doc.Children[0]; // here we assume the first node is a text node
t.text = "Hello World";
// NB: You need to explictly save the document (save will also close it)
doc.save();