@xilonglab/js-docx
v1.0.5
Published
A JavaScript library for processing DOCX files
Maintainers
Readme
@xilonglab/js-docx
A JavaScript library for processing DOCX files, providing utilities for reading, creating, and modifying Word documents.
Features
- Processors: Handle paragraph and table processing
- Utilities: Comprehensive set of tools for DOCX manipulation
- Template Engine: Built-in template rendering support
- XML Compilation: Convert custom XML syntax to WordprocessingML
Installation
npm install @xilonglab/js-docxUsage
Basic Example
import { Sheet, ParagraphProcessor, TableProcessor } from '@xilonglab/js-docx';
// Create a new sheet from a template
const sheet = new Sheet('template.docx');
// Render XML content
const xmlContent = `
<xl-p style="align:center;font-size:24">Hello World</xl-p>
<xl-table grid="3000/3000">
<xl-tr>
<xl-tc>Cell 1</xl-tc>
<xl-tc>Cell 2</xl-tc>
</xl-tr>
</xl-table>
`;
const renderedContent = sheet.renderTemplate(xmlContent, {});Processors
ParagraphProcessor
import { ParagraphProcessor } from '@xilonglab/js-docx';
// Compile xl-p to Word XML
const compiled = ParagraphProcessor.compile(xmlContent);
// Decompile Word XML to xl-p format
const decompiled = ParagraphProcessor.decompile(wordXml);TableProcessor
import { TableProcessor } from '@xilonglab/js-docx';
// Compile xl-table to Word XML
const compiled = TableProcessor.compile(xmlContent);
// Decompile Word XML to xl-table format
const decompiled = TableProcessor.decompile(wordXml);Utilities
Sheet
import { Sheet } from '@xilonglab/js-docx';
// Create sheet from template
const sheet = new Sheet('template.docx');
// Render and save
const xmlContent = '<xl-p>Hello World</xl-p>';
sheet.renderXml('document', xmlContent);
sheet.save('output.docx');WordFile
import { WordFile } from '@xilonglab/js-docx';
// Create Word file
const wordFile = new WordFile('template.docx');
// Add image
const imageBuffer = fs.readFileSync('image.png');
const imageId = wordFile.addImage(imageBuffer, 'image.png');
// Add XML content
wordFile.addXml('header', '<w:p><w:r><w:t>Header content</w:t></w:r></w:p>');
// Save
wordFile.save('output.docx');XmlCompiler
import { XmlCompiler } from '@xilonglab/js-docx';
const compiler = new XmlCompiler();
// Compile template with custom syntax
const template = '<xl-p>Hello ((name))!</xl-p>';
const data = { name: 'World' };
const result = compiler.renderTemplate(template, data);API Reference
Classes
- Sheet: Main class for handling Word document templates
- WordFile: Base class for Word document manipulation
- ParagraphProcessor: Handles paragraph processing and conversion
- TableProcessor: Handles table processing and conversion
- XmlCompiler: Template compiler with custom syntax support
- FakeZip: In-memory ZIP file manipulation
- RelationMixin: Handles Word document relationships
- XmlTree: XML tree manipulation utilities
Custom XML Syntax
The library supports a custom XML syntax for easier document creation:
<xl-p>: Paragraph element with style support<xl-span>: Text span with inline formatting<xl-table>: Table element with grid and styling<xl-tr>: Table row (with header support)<xl-tc>: Table cell with alignment and merge support<xl-th>: Table header row (converted to header tr)
Template variables:
((variable)): Variable interpolation($ code $): Control structures (if, for, etc.)
License
ISC
