Ask HN: Have you run Microsoft Word in headless mode?
For example, "a node.js server on a Windows machine that takes a file, saves it onto disk and runs a Visual Basic application on it which itself starts Microsoft Word headless to [perform some operation]" [1]
I need to manipulate Word documents that include accurate page numbers for elements like Table of Contents and List of Figures. These dynamic elements are populated on first open by Word (with user approval)
I'd like to automate all of this, and potentially manipulate the resulting .docx afterward.
From my initial research, doing this well requires processing by Word's layout engine.
I need this to be available as a web service to another application written in Python.
Other solutions I've considered include using LibreOffice in a headless mode, or a commonly mentioned but high priced solution [2] Aspose.words.
[1] https://news.ycombinator.com/item?id=17425712
[2] https://products.aspose.app/words
11 comments
[ 3.4 ms ] story [ 40.0 ms ] threadYou can automate in other ways, such as interacting with COM via a scripting language or development language client-side. That is supported. If you're dead-set on a web-based application, then yeah, go 3rd party.
[0] https://support.microsoft.com/en-us/topic/considerations-for...
I don't think client side manipulation is an option as the result needs to be made available directly via a web-based SaaS.
The licensing issue seems thorny. If this were as easy to set up and use as the comment I quoted suggested, we would need to get some validation that any end user performing "exports" had their own valid license to Word at the time of export.
I'm not sure what the secret sauce is in the third party libraries is that can't be done at a much more limited scope by manipulating the docx directly.
The most complex thing seems to be the determination of page numbers associated with various headings and the ability to refresh this on the fly.
I would suggest talking to a company like CDW who can assist with license terms or Microsoft Licensing -- don't take an HN or any other forum reply as a definitive answer. Microsoft typically doesn't let you get away 'cheap' when you use their software like this. And they've covered all of the bases you can think of to be cheap.
Since docx is 'open', any masochistic third party can write a library to interact with it -- or just take from the likes of LibreOffice and adapt it for server-side software. This avoids the Microsoft licensing discussion.
[0] https://www.microsoft.com/licensing/terms/product/ForallSoft...
Given the complexity of all the things you can do with Word and the immense feature set, if high fidelity is the goal, I don't believe using anything but actual Word and the real Word layout engine will give you that. You'll have to figure out if the licensing problem is one you can live with.
Unfortunately M$ Word is the only option. Can’t say I’m happy about it. But it is what it is.
LibreOffice cannot open even simple documents correctly ime, but ymmw.
In case the “user approval” isn’t a part of COM connection, I’d look for headful word instead with an AHK/etc script triggering it.
In my comparisons for page numbering for generated elements like table of contents etc. it seemed to work pretty well.
https://support.microsoft.com/en-us/office/command-line-swit...
"Word and Excel have extremely complete object models, available via COM Automation, which allow you to programmatically do anything. In many situations, you are better off reusing the code inside Office rather than trying to reimplement it. Here are a few examples..."
https://www.joelonsoftware.com/2008/02/19/why-are-the-micros...