New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

docxtemplater

Package Overview
Dependencies
Maintainers
0
Versions
344
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

docxtemplater

Generate docx, pptx, and xlsx from templates (Word, Powerpoint and Excel documents), from Node.js, the Browser and the command line

  • 3.60.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
135K
decreased by-6.37%
Maintainers
0
Weekly downloads
 
Created

What is docxtemplater?

Docxtemplater is an npm package that allows you to generate and modify Word documents (DOCX) by using templates. It is particularly useful for creating documents with dynamic content, such as invoices, reports, or any document that requires data to be inserted programmatically.

What are docxtemplater's main functionalities?

Template Rendering

This feature allows you to render a DOCX template by replacing placeholders with actual data. In this example, a template file 'template.docx' is read, and placeholders for 'firstName' and 'lastName' are replaced with 'John' and 'Doe', respectively. The modified document is then saved as 'output.docx'.

const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');

const content = fs.readFileSync('template.docx', 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip);

doc.setData({
  firstName: 'John',
  lastName: 'Doe'
});

doc.render();

const buf = doc.getZip().generate({ type: 'nodebuffer' });
fs.writeFileSync('output.docx', buf);

Conditional Content

Docxtemplater supports conditional content, allowing sections of a document to be included or excluded based on data. In this example, the 'showSection' variable controls whether a section of the document is displayed.

const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');

const content = fs.readFileSync('template.docx', 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip);

doc.setData({
  showSection: true
});

doc.render();

const buf = doc.getZip().generate({ type: 'nodebuffer' });
fs.writeFileSync('output.docx', buf);

Looping Over Data

This feature allows you to loop over arrays of data to generate repeated sections in a document. In this example, an array of items is used to populate a list in the document, with each item's name and price being inserted.

const Docxtemplater = require('docxtemplater');
const PizZip = require('pizzip');
const fs = require('fs');

const content = fs.readFileSync('template.docx', 'binary');
const zip = new PizZip(content);
const doc = new Docxtemplater(zip);

doc.setData({
  items: [
    { name: 'Item 1', price: 10 },
    { name: 'Item 2', price: 20 }
  ]
});

doc.render();

const buf = doc.getZip().generate({ type: 'nodebuffer' });
fs.writeFileSync('output.docx', buf);

Other packages similar to docxtemplater

Keywords

FAQs

Package last updated on 26 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc