DEV Community

Arnav Bansal
Arnav Bansal

Posted on • Updated on

How does one create MS Office documents programmatically?

Circumstances have conspired against me, and now I must generate Word documents programmatically. I carry with me the sword of JavaScript, although I fear it to be inadequate.

So, how does one young adventurer navigate this space? Must I enter the lair of Microsoft, and battle with the .NET dragon?

I eagerly await your wisdom if you've fought such beasts before.

Journey on.

Update, for future wanderers: I have avoided conflict with the .NET dragon. The sword of JavaScript proved adequate, specifically the package https://github.com/Ziv-Barber/officegen. Thanks @derekjhopper

Top comments (6)

Collapse
 
isaacdlyman profile image
Isaac Lyman • Edited

The app I built uses the docx NPM package to dynamically generate Word documents on a Node server. You can catch a peek of the API here, although it may not be very clear which pieces of code are for docx and which pieces of code are for dealing with the Quill delta objects I'm trying to convert.

Just a heads-up: the documents it generates look okay in MS Word, but cause LibreOffice to crash immediately.

Unfortunately there is no official package for doing this, nor is there one with a big open-source community supporting it, nor is the format simple enough that you could trivially hack it together yourself.

Could you get away with an RTF file or a PDF? You may find those less annoying.

Alternately, if you are generating lots of very similar files, there are a few NPM packages that make it really simple to start from a Word template and replace certain tokens (like {myTemplatedString}) with custom data.

If you absolutely, positively have to generate a DOCX file from scratch with custom formatting, then it can be done, but I have to warn you it will be a slow and painful process.

Collapse
 
ewbi profile image
ewbi

In addition to slaying the OpenXML beast, we have in the past also gen'd documents in HTML and RTF formats, both of which Word is quite good at opening and are both significantly easier to target. In fact, you can give an HTML file a DOC extension and/or return it from the server with a content-type of application/msword, and Word will happily open it.

The right approach will depend in part on how complex the documents are, whether they include complex elements like sections, headers/footers, columns, fields, etc., which suggests OpenXML (or a library to do the same), or if it's just paragraphs and styling, in which case HTML (or RTF) might suffice more easily.

Good luck!

Collapse
 
derekjhopper profile image
Derek Hopper

I recently had to generate an Excel file with Ruby. My guess is you're looking for something that can read and write OpenXML files. I had moderate success with github.com/randym/axlsx (it's a Ruby gem however).

For JavaScript, github.com/Ziv-Barber/officegen looks worthy of investigation, however I've never used it. Good luck!

Collapse
 
peter profile image
Peter Kim Frank

I love this prompt and intro. I wish you the best of luck on your upcoming battle and journey.
god speed

Collapse
 
unstuckdev profile image
The Unstuck Developer

There's the theory and then there's the practice. In theory, the format is open and documented. It's a simple zip file with some XML inside. In practice, Word has to deal with decades of legacy expectations that are baked into the software that typically generates them.

Libraries exist to generate Word files in JavaScript, though I can't vouch for any. Whether or not the output works consistently in all software that reads them is another matter.

Collapse
 
derekjhopper profile image
Derek Hopper

Great point. This has been my experience as well. We once generated a file that could be read by Numbers.app and Excel; however, couldn't be read by a piece of third party software.