2018-08-04 02:34:48 +00:00
|
|
|
# Welcome
|
|
|
|
|
2018-08-04 03:06:09 +00:00
|
|
|
## Installation
|
2018-08-04 02:34:48 +00:00
|
|
|
|
2023-06-15 20:30:59 +00:00
|
|
|
```terminal
|
2018-08-04 03:06:09 +00:00
|
|
|
npm install --save docx
|
2018-08-04 02:34:48 +00:00
|
|
|
```
|
2018-08-04 03:06:09 +00:00
|
|
|
|
2018-08-04 02:34:48 +00:00
|
|
|
Then you can `require` or `import` as usual:
|
|
|
|
|
2019-08-06 22:40:09 +00:00
|
|
|
```ts
|
|
|
|
const docx = require("docx");
|
2018-08-04 02:34:48 +00:00
|
|
|
```
|
|
|
|
|
2019-08-06 22:40:09 +00:00
|
|
|
```ts
|
2018-08-09 02:16:35 +00:00
|
|
|
import * as docx from "docx";
|
2019-08-06 22:40:09 +00:00
|
|
|
// or
|
|
|
|
import { ... } from "docx";
|
2018-08-04 02:34:48 +00:00
|
|
|
```
|
|
|
|
|
2018-08-04 03:06:09 +00:00
|
|
|
## Basic Usage
|
|
|
|
|
2019-08-06 22:40:09 +00:00
|
|
|
```ts
|
|
|
|
import * as fs from "fs";
|
|
|
|
import { Document, Packer, Paragraph, TextRun } from "docx";
|
2018-08-04 03:06:09 +00:00
|
|
|
|
2019-08-06 22:40:09 +00:00
|
|
|
// Documents contain sections, you can have multiple sections per document, go here to learn more about sections
|
|
|
|
// This simple example will only contain one section
|
2021-03-20 00:01:19 +00:00
|
|
|
const doc = new Document({
|
2022-07-06 15:35:18 +00:00
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
properties: {},
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Hello World"),
|
|
|
|
new TextRun({
|
|
|
|
text: "Foo Bar",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
new TextRun({
|
|
|
|
text: "\tGithub is the best",
|
|
|
|
bold: true,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2019-08-06 22:40:09 +00:00
|
|
|
});
|
2018-08-04 03:06:09 +00:00
|
|
|
|
|
|
|
// Used to export the file into a .docx file
|
2019-08-08 00:15:06 +00:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2019-08-06 22:40:09 +00:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
2019-01-02 16:20:11 +00:00
|
|
|
});
|
2018-08-04 03:06:09 +00:00
|
|
|
|
2020-08-08 00:40:11 +00:00
|
|
|
// Done! A file called 'My Document.docx' will be in your file system.
|
2018-08-04 03:06:09 +00:00
|
|
|
```
|
2018-08-04 02:34:48 +00:00
|
|
|
|
2018-09-12 10:02:42 +00:00
|
|
|
<p align="center">
|
2022-07-06 15:35:18 +00:00
|
|
|
<!-- cspell:disable-next-line -->
|
2020-09-05 17:44:49 +00:00
|
|
|
<img alt="clippy the assistant" src="./clippy.png">
|
2018-09-12 10:02:42 +00:00
|
|
|
</p>
|
|
|
|
|
2018-08-04 02:34:48 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
Made with 💖
|