Socket
Socket
Sign inDemoInstall

docx

Package Overview
Dependencies
9
Maintainers
1
Versions
82
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    docx

Generate .docx documents with JavaScript (formerly Office-Clippy)


Version published
Weekly downloads
125K
decreased by-8.98%
Maintainers
1
Install size
10.9 MB
Created
Weekly downloads
 

Package description

What is docx?

The docx npm package is a powerful library for creating and manipulating Microsoft Word documents programmatically. It allows developers to generate .docx files with various elements such as text, images, tables, and more, directly from their JavaScript or TypeScript code.

What are docx's main functionalities?

Creating a Simple Document

This feature allows you to create a simple Word document with text. The code sample demonstrates how to create a document with a single paragraph containing both regular and formatted text.

const { Document, Packer, Paragraph, TextRun } = require('docx');
const fs = require('fs');

const doc = new Document({
  sections: [
    {
      properties: {},
      children: [
        new Paragraph({
          children: [
            new TextRun('Hello World!'),
            new TextRun({
              text: ' This is bold and underlined text.',
              bold: true,
              underline: {} 
            })
          ]
        })
      ]
    }
  ]
});

Packer.toBuffer(doc).then((buffer) => {
  fs.writeFileSync('MyDocument.docx', buffer);
});

Adding Images

This feature allows you to add images to your Word document. The code sample demonstrates how to insert an image with specified dimensions into a document.

const { Document, Packer, Paragraph, ImageRun } = require('docx');
const fs = require('fs');

const doc = new Document({
  sections: [
    {
      properties: {},
      children: [
        new Paragraph({
          children: [
            new ImageRun({
              data: fs.readFileSync('path/to/image.png'),
              transformation: {
                width: 100,
                height: 100
              }
            })
          ]
        })
      ]
    }
  ]
});

Packer.toBuffer(doc).then((buffer) => {
  fs.writeFileSync('DocumentWithImage.docx', buffer);
});

Creating Tables

This feature allows you to create tables within your Word document. The code sample demonstrates how to create a table with two rows and two columns.

const { Document, Packer, Table, TableRow, TableCell, Paragraph } = require('docx');
const fs = require('fs');

const doc = new Document({
  sections: [
    {
      properties: {},
      children: [
        new Table({
          rows: [
            new TableRow({
              children: [
                new TableCell({
                  children: [new Paragraph('Cell 1')] 
                }),
                new TableCell({
                  children: [new Paragraph('Cell 2')] 
                })
              ]
            }),
            new TableRow({
              children: [
                new TableCell({
                  children: [new Paragraph('Cell 3')] 
                }),
                new TableCell({
                  children: [new Paragraph('Cell 4')] 
                })
              ]
            })
          ]
        })
      ]
    }
  ]
});

Packer.toBuffer(doc).then((buffer) => {
  fs.writeFileSync('DocumentWithTable.docx', buffer);
});

Other packages similar to docx

Readme

Source

clippy the assistant

Generate .docx files with JS/TS very easily, written in TS.


NPM version Build Status Dependency Status Known Vulnerabilities Chat on Gitter code style: prettier

NPM

docx

Install

$ npm install --save docx

Demo

Press endpoint on the RunKit website:

Run demos locally:
$ npm run demo

This command will run the demo selector app in the demo folder. It will prompt you to select a demo number, which will run a demo from that folder.

Guide

Please refer to the Wiki for details on how to use this library, examples and much more!

Full documentation can be found here: http://dolanmiu.github.io/docx/index.html

Simple Usage

// Used to create docx files
var docx = require("docx");

// Create document
var doc = new docx.Document();

// Add some content in the document
var paragraph = new docx.Paragraph("Some cool text here.");
// Add more text into the paragraph if you wish
paragraph.addRun(new docx.TextRun("Lorem Ipsum Foo Bar"));
doc.addParagraph(paragraph);

// Used to export the file into a .docx file
var exporter = new docx.LocalPacker(doc);

// Or use the express packer to make the file downloadable.
// res is express' Response object
var exporter = new docx.ExpressPacker(doc, res);

exporter.pack("My First Document");
// If you want to export it as a .pdf file instead
exporter.packPdf("My First Document");

// done! A file called 'My First Document.docx'
// will be in your file system if you used LocalPacker
// Or it will start downloading if you are using Express

Examples

Check the Wiki for examples.

Contributing

Read the contribution guidelines here.


Made with 💖

Huge thanks to @felipeochoa for awesome contributions to this project

Keywords

FAQs

Last updated on 24 Mar 2018

Did you know?

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc