48 lines
1.7 KiB
Markdown
48 lines
1.7 KiB
Markdown
![]() |
# Styling with XML
|
||
|
|
||
|
## Setup
|
||
|
|
||
|
1. Create a new word document in Microsoft Word
|
||
|
2. Customise the styles on the Ribbon Bar.
|
||
|
For example, modify the `Normal`, `Heading 1`, `Heading 2` like so:
|
||
|
|
||
|

|
||
|

|
||
|
|
||
|
3. You can even create a totally new `Style`:
|
||
|
|
||
|

|
||
|

|
||
|
|
||
|
4. Save
|
||
|
5. Re-name the saved `.docx` file to `.zip` and un-zip
|
||
|
6. Find `styles.xml`
|
||
|
|
||
|

|
||
|
|
||
|
## Usage
|
||
|
|
||
|
Read the styles using `fs`, and put it into the `Document` object in the constructor:
|
||
|
|
||
|
```js
|
||
|
const styles = fs.readFileSync("./styles.xml", "utf-8");
|
||
|
const doc = new docx.Document({
|
||
|
title: "Title",
|
||
|
externalStyles: styles,
|
||
|
});
|
||
|
```
|
||
|
|
||
|
You can use paragraphs, `heading1()`, `heading2()` etc and it will be styled according to your `styles.xml` created earlier. You can even use your new style you made by calling the `style` method:
|
||
|
|
||
|
```js
|
||
|
doc.createParagraph("Cool Heading Text").heading1();
|
||
|
|
||
|
let paragraph = new docx.Paragraph('This is a custom named style from the template "Cool New Style"');
|
||
|
paragraph.style("Cool New Style");
|
||
|
doc.addParagraph(paragraph);
|
||
|
|
||
|
doc.createParagraph("Some normal text");
|
||
|
```
|
||
|
|
||
|
Example: https://github.com/dolanmiu/docx/blob/master/demo/demo13.js
|