2

I'm attempting to produce an ODT document using Node.js, and I'm a little lost as to how I can, or whether templates are a suitable alternative.

The document I want to create is simple: table of contents, heading 1s, and paragraphs following each heading.

I have my book represented simply in program like:

const chapters = [
  {
    title: "It Begins", paragraphs: [
      "On the first day...",
      "Things were looking good...",
      "Blah blah blah..."
    ]
  },
  {
    title: "Second Chapter", paragraphs: [
      "Blah blah blah..."
    ]
  }
];

Ideally I'd just have something like

const doc = new ODTFile();
chapters.forEach(chapter => {
  doc.insert('Heading 1', chapter.title);
  chapter.paragraphs.forEach(para => doc.insert('paragraph', para));
});
doc.generateTableOfContents();
doc.writeFile('foo.odt');

Unfortunately there doesn't seem to be much in the way of LibreOffice/ODT libraries available for JS, and none that can request a table of contents be generated.

So my second idea was to maybe create an ODT template, and just pass in values to fill values the template requires. But finding documentation about how to do this has been tricky, especially with the LibreOffice website broken in terms of rendering and partly unreadable. I'm not really clear if templates can handle being passed arbitrary numbers of values or values that nest/pair this way.

GreenTriangle
  • 2,382
  • 2
  • 21
  • 35
  • Looking for the same - I noticed https://github.com/loveencounterflow/coffeelibre It seems like Python is more popular from https://stackoverflow.com/questions/11638170/what-language-do-i-need-to-write-macros-in-libre-office-calc and http://christopher5106.github.io/office/2015/12/06/openoffice-libreoffice-automate-your-office-tasks-with-python-macros.html - and even then not so well-documented. – Ben Creasy Nov 29 '18 at 07:07

1 Answers1

0

What if you use Pandoc? You can easily build a Markdown file and then convert it to ODT using Pandoc. There's even a pandoc wrapper in NPM (and another one).

In your example, you just need to write a file like this:

# It Begins

On the first day...

Things were looking good...

Blah blah blah...

# Second Chapter

Blah blah blah...

Pandoc even allows using a reference document (for changing default styles) and templates (for using Markdown metadata values as variables in the document).