2018-08-03 23:26:14 +01:00
|
|
|
# Welcome
|
|
|
|
|
2018-08-04 04:03:08 +01:00
|
|
|
## Installation
|
2018-08-04 03:28:27 +01:00
|
|
|
|
|
|
|
```sh
|
2018-08-04 04:03:08 +01:00
|
|
|
npm install --save docx
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
2018-08-04 04:03:08 +01:00
|
|
|
|
2018-08-04 03:28:27 +01:00
|
|
|
Then you can `require` or `import` as usual:
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
|
|
|
const docx = require("docx");
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
2019-08-03 13:42:24 +01:00
|
|
|
```ts
|
2018-08-09 03:13:16 +01:00
|
|
|
import * as docx from "docx";
|
2019-08-03 13:42:24 +01:00
|
|
|
// or
|
|
|
|
import { ... } from "docx";
|
2018-08-04 03:28:27 +01:00
|
|
|
```
|
|
|
|
|
2018-08-04 04:03:08 +01:00
|
|
|
## Basic Usage
|
|
|
|
|
2019-08-06 17:51:13 +01:00
|
|
|
```ts
|
2019-08-03 13:42:24 +01:00
|
|
|
import * as fs from "fs";
|
|
|
|
import { Document, Packer, Paragraph, TextRun } from "docx";
|
2018-08-04 04:03:08 +01:00
|
|
|
|
2019-08-03 13:42:24 +01: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-19 20:53:56 +00:00
|
|
|
const doc = new Document({
|
|
|
|
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,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
],
|
2021-08-29 09:43:09 +03:00
|
|
|
}],
|
2019-08-03 13:42:24 +01:00
|
|
|
});
|
2018-08-04 04:03:08 +01:00
|
|
|
|
|
|
|
// Used to export the file into a .docx file
|
2019-08-07 22:12:14 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2019-08-03 13:42:24 +01:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
2019-01-02 15:53:28 +00:00
|
|
|
});
|
2018-08-04 04:03:08 +01:00
|
|
|
|
2020-08-06 08:56:02 +00:00
|
|
|
// Done! A file called 'My Document.docx' will be in your file system.
|
2018-08-04 04:03:08 +01:00
|
|
|
```
|
2018-08-04 03:28:27 +01:00
|
|
|
|
2018-09-11 19:16:47 +01:00
|
|
|
<p align="center">
|
2020-09-05 18:41:24 +01:00
|
|
|
<img alt="clippy the assistant" src="./clippy.png">
|
2018-09-11 19:16:47 +01:00
|
|
|
</p>
|
|
|
|
|
2018-08-03 23:26:14 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
Made with 💖
|