Home Reference Source Repository

docx4js

docx4js is a javascript docx parser.

The original goal is to support docx, pptx, and xlsx, but it's a huge work, so I limited to docx so far.

In sake of performance, the implementation doesn't keep parsed structure. It only traverse docx content, and identify docx model, then call passed visitors one by one. No matter content, and styles, are all with the same stratigy. This method makes it do more with less memory.

There are lots of information in docx, but the client application usually only cares about part of them, such as content only, structure only, some styles, or some attributes. The client application is able to handle special word model by TYPE.

Attributes of word model usually affects styles, but I don't understand all of them, so I'm lazy just to iterate every attribute, and some unknown child elements, so client application is possible to catch all information you know.

Features

environment

identified models

style

API

require("docx4js") return a docx converter, which has following two functions.

load(file): return Promise resolved by parsed document

file is a file path string in nodejs, as for browser, it is a file from input[type=file].

Parsed Document interface

createVisitorFactory(factory, option)

It's to create a factory function that to create a visitor specific to word model types

Visitor

example

var docx4js=require("docx4js")
docx4j.load(fileInput.files[0]) // a file path in nodejs
    .then(function(doc){
        var nothingFactory=DOCX.createVisitorFactory()

        var textFactory=(function(){
            var visitor=[]
            visitor.visit=(function(){
                switch(this.model.type){
                case 'paragraph':
                    return this.push("\n\r")
                case 'text':
                    return this.push(this.model.getText())
                }
            }
            return DOCX.createVisitorFactory(function(wordModel){
                visitor.model=wordModel
            })
        )();

        var complexFactory=(function(){
            class Visitor{}
            class P extends Visitor{
                visit(){}
            }
            class Image extends Visitor{
                visit(){}
            }
            class Shape extends Visitor{
                visit(){}
            }
            class Text extends Visitor{
                visit(){}
            }

            return DOCX.createVisitorFactory({
                    'paragraph': P,
                    'image': Image,
                    'text': Text,
                    'shape': Shape
                })
        )();

        doc.parse(nothingFactory, textFactory, complexFactory)
    })

License

GPL