Convert Figma logo to code with AI

dolanmiu logodocx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.

4,192
473
4,192
130

Top Related Projects

7,241

A pure PHP library for reading and writing word processing documents

Generate docx, pptx, and xlsx from templates (Word, Powerpoint and Excel documents), from Node.js or the browser. Demo: https://www.docxtemplater.com/demo. #docx #office #generator #templating #report #json #generate #generation #template #create #pptx #docx #xlsx #react #vuejs #angularjs #browser #typescript #image #html #table #chart

Standalone Office Open XML files (Microsoft Office 2007 and later) generator for Word (docx), PowerPoint (pptx) and Excell (xlsx) in javascript. The output is a stream.

Quick Overview

dolanmiu/docx is a Node.js library for creating and editing .docx files (Microsoft Word documents) without any Microsoft Office dependencies. It provides a simple and intuitive API for programmatically generating Word documents, including support for various document elements such as paragraphs, tables, images, and styles.

Pros

  • Easy-to-use API for creating Word documents programmatically
  • No Microsoft Office dependencies, making it lightweight and cross-platform
  • Supports a wide range of document elements and formatting options
  • Active development and community support

Cons

  • Limited support for complex document layouts and advanced features
  • May not perfectly replicate all Microsoft Word functionalities
  • Performance can be slower for very large documents
  • Documentation could be more comprehensive for some advanced use cases

Code Examples

Creating a simple document with a paragraph:

import { Document, Packer, Paragraph, TextRun } from "docx";

const doc = new Document({
  sections: [{
    properties: {},
    children: [
      new Paragraph({
        children: [
          new TextRun("Hello World"),
          new TextRun({
            text: "Styled text",
            bold: true,
            italics: true
          })
        ],
      }),
    ],
  }],
});

Packer.toBuffer(doc).then((buffer) => {
  // Save the buffer to a file or send it as a response
});

Adding a table to the document:

import { Document, Table, TableRow, TableCell, Paragraph } from "docx";

const table = new Table({
  rows: [
    new TableRow({
      children: [
        new TableCell({
          children: [new Paragraph("Cell 1")],
        }),
        new TableCell({
          children: [new Paragraph("Cell 2")],
        }),
      ],
    }),
  ],
});

const doc = new Document({
  sections: [{
    children: [table],
  }],
});

Inserting an image:

import { Document, Paragraph, ImageRun } from "docx";
import fs from "fs";

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

Getting Started

  1. Install the library:

    npm install docx
    
  2. Import the necessary components:

    import { Document, Packer, Paragraph, TextRun } from "docx";
    
  3. Create a document:

    const doc = new Document({
      sections: [{
        children: [
          new Paragraph({
            children: [new TextRun("Hello World")],
          }),
        ],
      }],
    });
    
  4. Generate and save the document:

    Packer.toBuffer(doc).then((buffer) => {
      fs.writeFileSync("My Document.docx", buffer);
    });
    

Competitor Comparisons

7,241

A pure PHP library for reading and writing word processing documents

Pros of PHPWord

  • Written in PHP, making it ideal for PHP-based projects and web applications
  • Supports a wider range of document formats, including DOC, RTF, and ODT
  • More extensive documentation and examples available

Cons of PHPWord

  • Generally slower performance compared to docx
  • Less active development and fewer recent updates
  • More complex API, potentially requiring more code for simple tasks

Code Comparison

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

docx:

const doc = new Document();
doc.addParagraph('Hello World!');
Packer.toBuffer(doc).then((buffer) => {
  fs.writeFileSync('helloWorld.docx', buffer);
});

Both libraries allow for creating simple Word documents, but PHPWord's API is more verbose. docx, being JavaScript-based, can leverage promises for asynchronous operations, which may be beneficial in certain scenarios. However, PHPWord's broader format support and PHP integration make it a strong choice for PHP-centric projects.

Generate docx, pptx, and xlsx from templates (Word, Powerpoint and Excel documents), from Node.js or the browser. Demo: https://www.docxtemplater.com/demo. #docx #office #generator #templating #report #json #generate #generation #template #create #pptx #docx #xlsx #react #vuejs #angularjs #browser #typescript #image #html #table #chart

Pros of docxtemplater

  • More comprehensive templating features, including loops and conditions
  • Better support for complex document structures and formatting
  • Wider range of output formats, including PDF generation

Cons of docxtemplater

  • Steeper learning curve due to more complex syntax
  • Slightly larger package size and potential performance overhead
  • May require additional configuration for certain advanced features

Code Comparison

docxtemplater:

const doc = new Docxtemplater();
doc.loadZip(zip);
doc.setData({
    firstName: "John",
    lastName: "Doe"
});
doc.render();

docx:

const doc = new Document();
const paragraph = new Paragraph("Hello, World!");
doc.addParagraph(paragraph);
Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("My Document.docx", buffer);
});

Both libraries offer ways to create and manipulate Word documents, but docxtemplater focuses more on template-based document generation, while docx provides a more programmatic approach to building documents from scratch. docxtemplater excels in scenarios where you need to fill complex templates with data, while docx might be more suitable for generating simpler documents or when you need fine-grained control over document structure.

Standalone Office Open XML files (Microsoft Office 2007 and later) generator for Word (docx), PowerPoint (pptx) and Excell (xlsx) in javascript. The output is a stream.

Pros of officegen

  • Supports a wider range of Office document types (Word, PowerPoint, Excel)
  • Offers more flexibility in document creation and manipulation
  • Has a simpler API for basic document generation tasks

Cons of officegen

  • Less actively maintained compared to docx
  • Documentation is not as comprehensive or well-organized
  • May have fewer advanced features for complex Word document creation

Code Comparison

officegen:

var officegen = require('officegen');
var docx = officegen('docx');
var paragraph = docx.createP();
paragraph.addText('Hello, World!');

docx:

const docx = require('docx');
const doc = new docx.Document();
const paragraph = new docx.Paragraph('Hello, World!');
doc.addParagraph(paragraph);

Both libraries allow for creating simple Word documents with paragraphs and text. officegen uses a more method-chaining approach, while docx employs a more object-oriented structure. The officegen example is slightly more concise, but docx provides a clearer separation of document elements.

Overall, officegen offers broader Office suite support and simpler basic usage, while docx provides more robust features and better maintenance for complex Word document creation.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

clippy the assistant

Easily generate and modify .docx files with JS/TS. Works for Node and on the Browser.


NPM version Downloads per month GitHub Action Workflow Status Known Vulnerabilities PRs Welcome codecov

drawing

Demo

Browser

Here are examples of docx being used with basic HTML/JS in a browser environment:

Here are examples of docx working in Angular:

Here are examples of docx working in React:

Here is an example of docx working in Vue.js:

Node

Press endpoint on the RunKit website:

RunKit Instructions

More here

How to use & Documentation

Please refer to the documentation at https://docx.js.org/ for details on how to use this library, examples and much more!

Examples

Check the demo folder for examples.

Contributing

Read the contribution guidelines here.

Used by

drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing drawing

...and many more!


patreon browserstack

Made with 💖

NPM DownloadsLast 30 Days