Library that helps to work with .docx
files.
Essentially, it is a Clojure wrapper on Apache POI library.
The main idea is to use plain data instead of Apache POI classes to transform a .docx
document.
There are 2 types of transformations: append and replace.
It is possible to append and replace the following data types:
The latest version of docx-utils
is hosted on Clojars:
Main entrance function is docx-utils.core/transform
. It has 3 versions:
.docx
document, applies all transformations and returns a file path of the resulting document. Note that Resulting file is created in a temp folder and it will be deleted after the JVM exits. This function could/should be used to create a template docx
document.docx
is created and used as a template.nil
then error is thrown.Note that if a placeholder is not found in the template then nothing happens, only a log warning is produced.
The complete API documentation is also available (codox generated).
To replace a placeholder with a paragraph text:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :replace-text
:placeholder "%{PLACEHOLDER}"
:replacement "Standalone paragraph."}])
To replace a placeholder inside a text paragraph:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :replace-text-inline
:placeholder "%{PLACEHOLDER}"
:replacement "in-lined text"}])
To replace a placeholder with a data table
:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :replace-table
:placeholder "%{PLACEHOLDER}"
:replacement [["cell 11" "cell 12" "cell 13"]
["cell 21" "cell 22" "cell 23"]]}])
To replace a placeholder with a bulleted list:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :replace-bullet-list
:placeholder "%{PLACEHOLDER}"
:replacement ["item 1" "item 2" "item 3"]}])
To replace a placeholder with a numbered list:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :replace-numbered-list
:placeholder "%{PLACEHOLDER}"
:replacement ["item 1" "item 2" "item 3"]}])
To replace a placeholder with an image:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :replace-image
:placeholder "%{PLACEHOLDER}"
:replacement "/path/to/image/file.jpg"}])
To append a text paragraph to the end of the template document:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :append-text
:replacement "Text paragraph."}])
To append a text snippet to the end of the last paragraph of the template document:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :append-text-inline
:replacement "text snippet"}])
To append a data table to the end of the template document:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :append-table
:replacement [["cell 11" "cell 12" "cell 13"]
["cell 21" "cell 22" "cell 23"]]}])
To append a bullet list to the end of the template document:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :append-bullet-list
:replacement ["item a" "item b" "item c"]}])
To append a numbered list to the end of the template document:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :append-numbered-list
:replacement ["item a" "item b" "item c"]}])
To append an image to the end of the template document:
(docx-utils.core/transform "/path/to/template/file.docx"
[{:type :replace-image
:replacement "/path/to/image/file.jpg"}])
:replacement
could be either a String
or a map. If String
then the value is pasted into the document without any additional formating (the formating of the placeholder is preserved), if a map is provided the underlying Run
is formated accordingly to the options provided in a map, e.g. {:bold true :text "Bolded text"}
.:type
value should be somehow taken from a list of values. Maybe a namespace with constants? e.g. (def REPLACE_TEXT :replace-text)
. Or Java enums?Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close