
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
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.
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);
});
officegen is another npm package that allows you to generate Microsoft Office documents, including Word, Excel, and PowerPoint files. Compared to docx, officegen offers a broader range of document types but may not have as many advanced features specifically for Word documents.
mammoth is a library focused on converting .docx documents to HTML and plain text. While it doesn't offer the same document creation capabilities as docx, it is useful for extracting content from existing Word documents.
pizzip is a low-level library for manipulating ZIP files, which can be used in conjunction with other libraries like docxtemplater to create and modify .docx files. It provides more control over the ZIP structure but requires more effort to use for document creation.
Easily generate and modify .docx files with JS/TS. Works for Node and on the 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
:
Press endpoint
on the RunKit
website:
More here
Please refer to the documentation at https://docx.js.org/ for details on how to use this library, examples and much more!
Check the demo folder for examples.
Read the contribution guidelines here.
...and many more!
Made with 💖
FAQs
Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
The npm package docx receives a total of 0 weekly downloads. As such, docx popularity was classified as not popular.
We found that docx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.