Populating Multiple Labels in .docx Files using docxtemplater in Node.js

46 Views Asked by At

I am working on a Node.js project where I need to edit .docx files, specifically using the docxtemplater library. I have a structured .docx file that I cannot modify, and I'm facing challenges with efficiently populating labels

Here's a simplified representation of the document structure:

[DATE] | WHAT WE DISCUSS
Chairperson: [Name]
[TOPIC][Name]...
[TOPIC][Name]...
... [TOPIC] ... [Name] ...
Opening words (1 min.)
...
[DATE] | WHAT WE DISCUSS
Chairperson: [Name]
... [TOPIC] ... [Name] ...
... [TOPIC] ... [Name] ...
Opening words (1 min.) [Name]

The document contains recurring labels such as [DATE], [TOPIC], [Name], and more. For instance, [DATE] may occur multiple times, and I need to dynamically populate each occurrence with a unique value. I am considering the following approach: javascript

let dates = ['1/1/2020', '8/1/2020', '15/1/2020', '22/1/2020', '28/1/2020'];
doc.render({
  ...
  'DATE': () => {return dates.shift()},
  ...
});

I found the following solution, but I doubt its efficiency. If I start filling [NAME], how can I create an array of names and ensure that each name corresponds to the correct field?

Is there another way using the docxtemplater library to achieve multiple label replacements with unique values? or another library?

0

There are 0 best solutions below