officeparser
v8.9.0
Published
A robust, strictly-typed Node.js and Browser library for parsing office files (.docx, .pptx, .xlsx, .odt, .odp, .ods, .odg, .pdf, .rtf, .csv, .md, .html, .epub, .tex) and generating high-fidelity outputs in DOCX, ODT, XLSX, ODS, PPTX, ODP, LaTeX, Markdown
Downloads
7,110,531
Maintainers
Readme
officeParser: Universal Office Document Parser & Generator
A robust, strictly-typed Node.js and Browser library for parsing office files into a rich Abstract Syntax Tree (AST) and generating high-fidelity output in multiple formats.
Parses: docx · pptx · xlsx · odt · odp · ods · odg · pdf · rtf · csv · md · html · epub · tex (LaTeX, including Overleaf project zips)
Generates: DOCX · ODT · XLSX · ODS · PPTX · ODP · LaTeX · Markdown · HTML · CSV · RTF · PDF · EPUB · Plain Text · RAG Chunks
🌟 Live Interactive AST Visualizer & Documentation 🌟
Upload any office file in your browser: inspect the AST, tweak config, and preview generated output in real-time.
- AST Visualizer: Inspect the hierarchical node tree, metadata, and raw content
- Config Configurator: Tweak options (
ignoreNotes,ocr,newlineDelimiter) and see results instantly - Debugging: Identify exactly how nodes are interpreted
- Format Specs: Read detailed specs for the AST structure and all config options
📝 Changelog
What's New in v8
- Rebuilt PDF text extraction. PDF is no longer treated as a page of flat lines. Tagged PDFs now yield real
heading(with correct levels),table/row/cell,listand footnote/endnotenotenodes, and oneparagraphper paragraph. Untagged PDFs recover the same structure geometrically. Multi-column and float-beside-text pages are read in the correct order (recursive XY-cut), broken and glued words are fixed from inter-fragment spacing, super/subscripts and hyphenated line-breaks are rejoined, rotated text is recovered, and internal links resolve to the target section..to('text')is layout-faithful by default, rendering each page as a spatial grid so columns and tables line up like the source. Per-run color/highlight extraction (on by default; setpdfParserConfig.extractTextColor: falseto skip it) and merged-cell (colSpan/rowSpan) recovery round it out, and every node carries page geometry (bounds). - Password-protected documents. Encrypted PDF, OOXML (
docx/xlsx/pptx) and ODF (odt/ods/odp/odg) open through one unifiedpassword/onPasswordoption, across parsing, conversion and templating. - Native DOCX & ODT generation, plus a native PDF engine (
pdfConfig.engine: 'native', built onpdf-lib) that produces real PDF bytes with no headless browser, in Node and the browser alike. - Templates / mail-merge via
OfficeTemplate.render(fill a Word or OpenDocument (.docx,.odt,.ott) template's{{placeholders}},{{ if }}conditions and{{ for }}loops, down to one table row per item, values formatted with Word-style pictures, single or batch;OfficeTemplate.tags()lists its tags andOfficeTemplate.check()checks the data first), and ODG parsing (LibreOffice Draw). - LaTeX in both directions (8.1):
.texfiles and Overleaf project zips parse into the same AST as every other format (sections, lists, tables with merged cells, figures, math, footnotes, citations, cross-references, user macros,beamerslides), so LaTeX converts to DOCX, ODT, HTML, Markdown and the rest. Andto('tex')turns any parsed document into LaTeX source that compiles unmodified with pdfLaTeX, XeLaTeX, LuaLaTeX, upLaTeX, pLaTeX andlatex(the last three through dvipdfmx), presentations included (asbeamerframes), carrying its images inside the one.texfile (or, in bundle mode, as files in a zip). See LaTeX Support. - XLSX, ODS, PPTX and ODP output (8.3):
to('xlsx')andto('ods')write a workbook (a spreadsheet sheet for sheet, its values typed and shown in their number formats; any other document as its tables, each a sheet, and its text on a sheet of its own), andto('pptx')andto('odp')write a deck (a presentation slide for slide; a PDF's pages as slides with each paragraph where the page set it; any other document flowed onto slides, a slide for each heading). See XlsxGeneratorConfig and PptxGeneratorConfig.
See the full changelog for the complete list, including breaking changes.
Table of Contents
- What's New in v8
- Install
- Command Line Usage
- Quick Decision Guide
- Library Usage: Parsing
- OfficeGenerator
- OfficeConverter: One-Step API
- OfficeTemplate: Mail-Merge / Document Generation
- Native RAG Chunking
- The AST Structure
- Deep Dive: Document Components
- Markdown Dialect Support
- EPUB Support
- LaTeX Support
- Performance Highlights
- Advanced AST Usage
- Configuration Reference
- OfficeParserConfig
- GeneratorConfig (Common)
- onNode Callback
- styleMap: Semantic Style Mapping
- HtmlGeneratorConfig
- MdGeneratorConfig
- PdfGeneratorConfig
- DocxGeneratorConfig
- OdtGeneratorConfig
- XlsxGeneratorConfig
- OdsGeneratorConfig
- PptxGeneratorConfig
- OdpGeneratorConfig
- TexGeneratorConfig
- CsvGeneratorConfig
- TextGeneratorConfig
- metadataOverrides
- OfficeConverterConfig
- ChunkingConfig
- OCR Scheduler & Resource Management
- Browser Usage
- Troubleshooting & Common Issues
- Known Limitations
- Security & Trust Boundary
- Contributing
Install via npm
npm i officeparser[!NOTE] Requires Node.js 22.23.4, 24.22.1, 26.12.1 or later in its line. Earlier releases have a flaw in their Brotli decoder (CVE-2026-111497); officeParser avoids that decoder on them and raises
BROTLI_DECODER_AVOIDEDwhen it does, but only upgrading protects the rest of your application.
Command Line Usage
# Full AST as JSON (default)
npx officeparser /path/to/file.docx
# Plain text output
npx officeparser /path/to/file.docx --to=text
# Convert DOCX to Markdown and save
npx officeparser report.docx --to=md --output=report.md
# Convert PPTX to HTML with OCR (OCR runs over extracted images, so --extractAttachments is required)
npx officeparser presentation.pptx --to=html --output=preview.html --ocr --extractAttachments
# Convert XLSX to CSV with a custom delimiter
npx officeparser data.xlsx --to=csv --csvDelimiter=";"
# Generate RAG chunks
npx officeparser document.pdf --to=chunks
# Convert DOCX to EPUB (--extractAttachments is required to embed images)
npx officeparser book.docx --extractAttachments --to=epub --output=book.epub
# Convert Markdown (or any source) to a Word document
npx officeparser notes.md --extractAttachments --to=docx --output=notes.docx
# Convert a Word document (or any source) to OpenDocument Text
npx officeparser report.docx --extractAttachments --to=odt --output=report.odt
# Convert a document's tables to a workbook (each table a sheet, its text on a sheet named Text)
npx officeparser report.docx --to=xlsx --output=report.xlsx
npx officeparser report.pdf --to=ods --odsConfig.sheetPerTable=false --output=report.ods
# Convert a PDF or any other source to a presentation (--extractAttachments is required for pictures and charts)
npx officeparser slides.pdf --extractAttachments --to=pptx --output=slides.pptx
npx officeparser slides.pdf --extractAttachments --to=pptx --pptxConfig.backgrounds=page1.png,page2.png --output=slides.pptx
# Convert any source to LaTeX: a .tex file, or a zip of main.tex plus its images
npx officeparser paper.docx --extractAttachments --to=tex --output=paper.tex
npx officeparser paper.docx --extractAttachments --to=tex --texConfig.bundle --output=paper.zip
# Convert LaTeX to Word: a single .tex, or an Overleaf project zip (which brings its \input files and images)
npx officeparser paper.tex --to=docx --output=paper.docx
npx officeparser overleaf-project.zip --extractAttachments --to=docx --output=paper.docx
# Overriding file extension mapping
npx officeparser my_document --fileType=docx --to=jsonCLI Syntax
- Values: Flags can be passed as
--flag=valueor--flag value. - Booleans: Bare flags imply
true(e.g.--ocris equivalent to--ocr=true). Negation flags start withno-(e.g.--no-ocris equivalent to--ocr=false). - Nested Objects: You can pass nested properties directly using JSON dot-notation (e.g.
--ocrConfig.language=fraor--htmlConfig.containerWidth=900px). - Images: the CLI parses directly, so add
--extractAttachmentsfor images to reach any output (HTML/EPUB embed them, DOCX/ODT/PPTX/ODP/Markdown/native-PDF include them, and LaTeX carries PNG and JPEG images inside the.tex, or with--texConfig.bundlepackages them beside it). Without it, an image node has no bytes and HTML/Markdown emit a name-only<img src="image1.png">reference. (TheOfficeConverter/convert()API auto-enables this; the CLI does not.)
CLI Options
| Flag | Values | Default | Description |
|------|--------|---------|-------------|
| --to | json\|text\|md\|html\|csv\|rtf\|pdf\|docx\|odt\|xlsx\|ods\|pptx\|odp\|tex\|epub\|chunks | json | Output format (latex is accepted as an alias of tex) |
| --output | path | (none) | Write output to a file |
| --fileType | docx\|xlsx\|pptx\|odt\|odp\|ods\|odg\|pdf\|rtf\|csv\|md\|html\|epub\|tex | (none) | Explicitly override input file type detection. Also accepts latex/ltx, the ODF template names ott/ots/otp/otg, Word's, Excel's and PowerPoint's macro-enabled, template and slideshow names (docm/dotx/dotm, xlsm/xltx/xltm, pptm/potx/potm/ppsx/ppsm), and zip (parsed as whatever the archive holds) |
| --ocr | boolean | false | Enable OCR for images (also requires --extractAttachments; OCR runs over extracted images) |
| --ocrConfig.language | string | eng | Tesseract language(s), e.g. deu or eng+fra |
| --ocrConfig.preserveLayout | boolean | true | Keep the line layout of recognized text |
| --password | string | (none) | Password for an encrypted document (PDF, OOXML, or ODF) |
| --allowRemoteAssets | boolean | true | false fails with REMOTE_ASSET_BLOCKED where the pdf.js worker or Tesseract's files would come from the library's default CDN address, and fetches no pdf.js CMap or image decoder from it (PDF_ASSET_UNAVAILABLE) |
| --pdfCMapUrl | string | pdfjs-dist's cmaps/ | Folder of pdf.js's CMaps, for the text of fonts a PDF does not embed (CJK) |
| --pdfDecoderUrl | string | pdfjs-dist's wasm/ | Folder of pdf.js's image decoders (JBIG2, CCITT fax, JPEG 2000) |
| --ocrConfig.cacheMethod | write\|readOnly\|refresh\|none | write | Where OCR keeps the language data it loads (none stores nothing) |
| --extractAttachments | boolean | false | Extract images/charts as Base64 |
| --ignoreNotes | boolean | false | Ignore footnotes/endnotes/speaker notes |
| --ignoreComments | boolean | false | Ignore inline comments |
| --ignoreHeadersAndFooters | boolean | false | Ignore headers and footers |
| --ignoreSlideMasters | boolean | false | Ignore slide masters |
| --ignoreInternalLinks | boolean | false | Ignore internal links |
| --newlineDelimiter | string | \n | Delimiter between lines/blocks in plaintext outputs |
| --csvDelimiter | string | auto | Delimiter of CSV files; auto reads it from the file (comma, semicolon, tab or bar) |
| --includeRawContent | boolean | false | Include raw XML/RTF in nodes |
| --serializeRawContent | boolean | true | Include stringified XML in metadata |
| --preserveXmlWhitespace | boolean | false | Keep raw formatting space |
| --includeBreakNodes | boolean | false | Include layout break nodes (DOCX, ODF and LaTeX page and column breaks; a typed line break is always kept) |
| --ignorePageGeometry | boolean | false | Omit per-node bounding boxes and page dimensions |
| --pdfParserConfig.useTags | boolean | true | Use the PDF tag tree; false forces geometry-only structure |
| --pdfParserConfig.detectColumns | boolean | true | Multi-column reading-order detection |
| --pdfParserConfig.pageRange | string | all | Parse only the given pages, e.g. 1-3,7 |
| --htmlParserConfig.preserveComments | boolean | false | Keep HTML/EPUB <!-- --> comments as comment nodes |
| --texParserConfig.today | string | the date of the parse | What \today prints in LaTeX input, e.g. --texParserConfig.today="May 1, 2024" |
| --pdfParserConfig.headingDetection | auto\|font-size\|tags\|off | auto | How PDF headings are decided: from tags and a font-size heuristic, the heuristic alone, tags alone, or never |
| --pdfParserConfig.mergeHyphenatedWords | boolean | true | Rejoin words hyphenated across line breaks |
| --pdfParserConfig.normalizeText | boolean | true | Unicode/ligature normalization of extracted text |
| --pdfParserConfig.extractTextColor | boolean | true | Record each run's fill colour in formatting.color, and the coloured rectangle it sits on in formatting.backgroundColor (set false to skip for speed) |
| --pdfParserConfig.keepHeadersAndFootersInPages | boolean | false | Keep a PDF's running headers and footers (a tagged PDF's artifacts, or the lines an untagged one repeats at the top and bottom of its pages) in each page's content, in place, instead of auxiliary |
| --pdfParserConfig.maxTextItems | number | 20000 | Base of the text items a PDF may yield (plus one per byte of the file) |
| --pdfParserConfig.maxOperators | number | 250000 | Base of the drawing operators kept from a PDF (plus four per byte of the file) |
| --pdfParserConfig.maxAnnotations | number | 10000 | Base of the annotations read from a PDF (plus one per 32 bytes of the file) |
| --pdfParserConfig.maxTimeMs | number | 5000 | Base of the CPU time the separate pdf.js process may spend on a PDF (plus 20 ms per KB of the file) |
| --pdfParserConfig.maxImagePixels | number | 40000000 | Most pixels a PDF picture may have to be extracted |
| --pdfParserConfig.separateProcess | boolean | true | Run pdf.js in a separate process under a memory limit (Node) |
| --pdfParserConfig.processMemoryMb | number | 1024 | Heap the separate pdf.js process may use |
| --verbose | boolean | false | Show full error stack traces and warning logs |
| --includeFormatting | boolean | true | Include formatting style map matching |
| --renderMetadata | boolean | false | Render metadata as visible content in the generated output |
| --includeImages | image-only\|image+ocr-text\|ocr-text-only\|none | image-only | How image nodes render. Works as --includeImages=<mode> or --includeImages <mode>; a bare --includeImages means image-only |
| --maxInlineImageBytes | number | 1500000 | Largest image HTML/Markdown inlines as a data: URI (0 never inlines) |
| --htmlConfig.containerWidth | string | number | auto | HTML output container width (e.g. 900px, 100%) |
| --textConfig.pageSeparator | string | \n | Separator written between pages in text output |
| --pdfConfig.engine | html\|native | html | PDF engine: Puppeteer (html) or pdf-lib (native, no browser) |
| --pdfConfig.fonts.regular | file path | (none) | Font file (.ttf/.otf/.woff/.woff2) the native engine draws in; also .bold, .italic, .boldItalic, .mono |
| --pdfConfig.fallbackFonts | file paths, comma-separated | (none) | Font files tried in order for the characters the fonts before them lack (native engine) |
| --pdfConfig.subsetFonts | boolean | true | false embeds whole font files, not only the glyphs used |
| --pdfConfig.outline | boolean | false | Write a bookmark outline from the headings |
| --pdfConfig.format | auto\|A4\|Letter\|... | auto | Paper format; auto is the document's own pages (each section's, each sheet's, a slide's size), else A4 |
| --pdfConfig.printHidden | boolean | false | Print hidden sheets, rows, columns and slides |
| --pdfConfig.printSpeakerNotes | boolean | false | Print each slide's speaker notes after it |
| --xlsxConfig.sheetPerTable | boolean | true | XLSX: each of a document's tables on a sheet of its own; false puts them all on one sheet. --odsConfig.sheetPerTable for ODS |
| --xlsxConfig.includeText | boolean | true | XLSX: a document's text outside its tables on a sheet named Text; false leaves it out. --odsConfig.includeText for ODS |
| --pptxConfig.backgrounds | picture file paths, comma-separated | (none) | PPTX: a PNG or JPEG file for each page of a PDF (or slide of a presentation), in order, drawn as its slide's background under its text; an empty entry for a page without one (a.png,,c.png). A file that cannot be read ends the run. --odpConfig.backgrounds for ODP |
| --texConfig.bundle | boolean | false | LaTeX: write a zip of main.tex plus its images/ instead of the .tex alone |
| --texConfig.embedImages | boolean | true | LaTeX: false refers to images/ files instead of carrying PNG and JPEG images inside the .tex |
| --texConfig.documentClass | auto\|article\|report\|book\|beamer | auto | LaTeX document class (auto = beamer for presentations, article otherwise) |
| --texConfig.standalone | boolean | true | LaTeX: false writes the body only, for pasting into an existing document |
| ~~--format~~ | json\|text\|md\|html\|csv\|rtf\|pdf\|docx\|odt\|xlsx\|ods\|pptx\|odp\|tex\|epub\|chunks | json | Deprecated. Use --to |
| ~~--toText~~ | | | Removed in v8. Use --to=text. |
| ~~--ocrLanguage~~ | | | Removed in v8. Use --ocrConfig.language. |
| ~~--putNotesAtLast~~ | | | Removed in v8. Notes are attached structurally via node.notes. |
| ~~--outputErrorToConsole~~ | | | Removed in v8. Use --verbose. |
Every removed flag above exits with status 1 and prints its replacement, rather than being accepted
and ignored. An unrecognized or renamed config key (say --ocrConfig.autoTerminateTimeout) is not fatal, but the
CLI always prints the warning naming its replacement, with or without --verbose.
Quick Decision Guide
| Goal | API to use |
|------|-----------|
| Extract text / AST from a file | OfficeParser.parseOffice(file) |
| Convert directly to another format | OfficeConverter.convert(file, 'md') |
| Parse first, then generate | parseOffice() → OfficeGenerator.generate(ast, 'html') |
| Convert on the AST itself (shorthand) | ast.to('md') |
| RAG pipeline chunking | OfficeConverter.convert(file, 'chunks', {...}) |
Library Usage: Parsing
Async/Await
const officeParser = require('officeparser');
const ast = await officeParser.parseOffice('/path/to/file.docx');
console.log(ast.type); // 'docx'
console.log(ast.metadata); // { author, title, created, ... }
console.log(ast.content); // Array of hierarchical nodes
console.log(ast.attachments);// Images/charts (if extractAttachments: true)
console.log(ast.warnings); // Non-fatal issues from parsing phaseTypeScript (named import):
import { OfficeParser } from 'officeparser';
const ast = await OfficeParser.parseOffice('report.docx', {
extractAttachments: true,
ocr: true,
});Callback (Backward Compat)
officeParser.parseOffice('/path/to/file.docx', async function(ast, err) {
if (err) { console.error(err); return; }
console.log((await ast.to('text')).value);
});File Buffers, ArrayBuffers & Blobs
Pass a Buffer, ArrayBuffer, Uint8Array, or a web Blob/File instead of a file path:
const fs = require('fs');
const buffer = fs.readFileSync('/path/to/file.pdf');
const ast = await officeParser.parseOffice(buffer);In the browser you can hand a File/Blob straight from an <input type="file">, with no need to
read it into a buffer first. A File's name drives type detection, so no fileType hint is
needed when the name has a recognizable extension:
// input.files[0] is a File (e.g. "report.docx")
const ast = await officeParser.parseOffice(input.files[0]);[!IMPORTANT] Text-based formats from buffers need a
fileTypehint. Formats likemd,html,csvandtexhave no magic bytes, so the parser cannot auto-detect them from a buffer. You must providefileTypein that case:const ast = await officeParser.parseOffice(markdownBuffer, { fileType: 'md' });One exception: XHTML, an HTML page written as XML (an XML declaration before
<html xmlns="http://www.w3.org/1999/xhtml">, as office suites export HTML), is recognised by itshtmlroot element and read as HTML with or without the hint, and with no type-mismatch warning.
[!NOTE] ZIP-backed formats are identified from inside the archive. DOCX, XLSX, PPTX, ODT, ODS, ODP and EPUB are all ZIP files, and telling them apart from the first bytes alone is unreliable for archives written by streaming producers or holding very many parts. When the byte signature is inconclusive, the archive is opened and the format is read from its own declaration (
[Content_Types].xml, or themimetypeentry), so these parse from a buffer without a hint. A LaTeX project zip is recognized the same way, by a.texfile with a\documentclassnear the archive root, and so is a file named.zip: that extension names no format, so the archive's contents decide which parser runs. Word's, Excel's and PowerPoint's macro-enabled files, templates and slideshows (.docm,.dotx,.dotm,.xlsm,.xltx,.xltm,.pptm,.potx,.potm,.ppsx,.ppsm) are packaged as their documents are, and are read as DOCX, XLSX and PPTX, by name or from their bytes; their macros are not read. SupplyingfileTyperemains the fastest and most certain route: it decides which parser runs, and for these formats no archive inspection is done at all.
Cancellation with AbortSignal
You can pass a standard AbortSignal (e.g. from an AbortController) to cancel an active parse operation. This is especially useful for setting request-level timeouts or canceling long-running parses (like large PDFs with OCR).
const controller = new AbortController();
// Cancel parsing if it takes longer than 5 seconds
setTimeout(() => controller.abort(), 5000);
try {
const ast = await officeParser.parseOffice('large_scanned_file.pdf', {
abortSignal: controller.signal,
ocr: true,
extractAttachments: true // page-image OCR needs this; ocr alone does nothing
});
} catch (err) {
if (err.name === 'AbortError') {
console.log('Parsing was cancelled.');
} else {
console.error('Parsing failed:', err);
}
}The signal is checked between steps, and it stops work that waits: OCR, PDF pages, reading an
archive. Reading a document's own markup (its XML, HTML, Markdown or LaTeX) is synchronous work, and
a timer cannot fire while it runs, so an abort requested during it takes effect only once it ends.
To bound the time an untrusted document can take, parse it in a worker thread or child process and
end that when your time limit passes; the signal alone cannot. PDFs are the exception in Node: pdf.js
runs in a separate process (pdfParserConfig.separateProcess), which the signal ends mid-stream, and
pdf.js in the host stops at its next time slice.
[!IMPORTANT] AbortError Propagation When parsing is cancelled via
AbortSignal, the parser rejects with a standardAbortError(aDOMExceptionor an Error withname: 'AbortError'). This error is not wrapped in standard OfficeParser error types so that you can reliably detect cancellation usingerror.name === 'AbortError'.
[!NOTE] Cancellation is cooperative The signal is checked between steps: an already-aborted signal rejects before any work, and an abort is seen at the next check: between archive reads (an archive yields to the event loop every megabyte when a signal is given), pages, OCR jobs or batches of parsed tokens. Format detection and OpenDocument decryption stop too, and decryption asks for no other password after an abort. A step that runs synchronously, such as parsing a
.texfile or one large XML part, finishes before a timer's abort can run; those steps are bounded in size instead, andabortSignalis not a hard execution-time limit.
[!NOTE] Worker Cleanup on Abort If an OCR job is actively running in the background when the signal is aborted,
officeParserautomatically terminates the Tesseract worker process immediately and removes it from the pool to prevent thread/memory leaks.
Custom OCR Timeouts
To prevent the parser from hanging indefinitely due to slow network connections (when downloading Tesseract language datasets) or complex image processing, you can configure granular timeouts under ocrConfig.timeout.
const ast = await officeParser.parseOffice('scanned_document.pdf', {
ocr: true,
extractAttachments: true, // required: OCR of a PDF's page images runs through the attachment path
ocrConfig: {
timeout: {
workerLoad: 30000, // 30s max to load worker & download language training files
recognition: 15000, // 15s max per image text recognition
autoTerminate: 10000 // 10s of inactivity before terminating idle workers
}
}
});[!TIP] Non-Fatal Timeout Recovery If
workerLoadorrecognitiontimeouts are exceeded, the parser will log a warning inast.warningsand continue parsing the rest of the document. The overall promise resolves successfully with the text extracted from the document layers (rather than failing the entire parse).
OCR Layout Reconstruction
By default (ocrConfig.preserveLayout: true) the recognized text keeps its two-dimensional page layout, rebuilt from Tesseract's per-word bounding boxes: a scanned table, form or multi-column page keeps its columns (right-hand text stays on the right, labels and values line up) instead of collapsing to a flat reading-order string. It is the OCR analogue of textConfig.preserveLayout for born-digital PDFs. Set it false for the plain, linearized text.
const ast = await officeParser.parseOffice('scanned_invoice.pdf', {
ocr: true,
extractAttachments: true, // required: OCR of a PDF's page images runs through the attachment path
ocrConfig: { preserveLayout: true } // default; false = flat reading-order text
});ast.to(): Generate from AST
The preferred way to convert a parsed AST to another format. Returns a ConversionResult.
// ConversionResult shape:
// { value: string | Uint8Array | OfficeChunk[], messages: OfficeIssue[] }
const { value: markdown, messages } = await ast.to('md');
const { value: html } = await ast.to('html', { includeFormatting: false });
const { value: chunks } = await ast.to('chunks', { chunksConfig: { strategy: 'fixed-size', chunkSize: 800 } });
const { value: pdfBytes } = await ast.to('pdf'); // Uint8Array
const { value: deck } = await ast.to('pptx'); // Uint8Array (also docx, odt, xlsx, ods, odp, epub).to('text'): Plain Text Extraction
Plain text comes from .to('text'), which is asynchronous and configurable. Its defaults render
tables as aligned grids, lists with markers and indentation, and include notes and image
placeholders:
// Default: aligned tables, list markers, notes, image placeholders, layout-faithful PDF pages
const { value } = await ast.to('text');
// Flat stream of text, no grid alignment or markers
const { value } = await ast.to('text', {
includeImages: false,
textConfig: { preserveLayout: false, renderNotes: false },
});| Feature | default | flat (preserveLayout: false) | governed by |
|---|---|---|---|
| Tables | aligned grid | one cell per line, tab-separated | textConfig.preserveLayout (default true) |
| Lists | markers + indentation | plain text | textConfig.preserveLayout (default true) |
| PDF pages | spatial monospace grid (columns/tables aligned like the page) | flowing text | textConfig.preserveLayout + geometry |
| Footnotes/endnotes | emitted | emitted | textConfig.renderNotes (default true) |
| Image placeholders | emitted | emitted | includeImages (default true) |
For PDFs with page geometry (the default, unless ignorePageGeometry is set), preserveLayout renders
each page as a spatial monospace grid so multi-column text and tables line up much like the original
page, similar to pdftotext -layout. Use textConfig.pageSeparator (default '\n', or '\f' for a
form feed) to control what goes between pages.
Spreadsheets (CSV/ODS/XLSX) are unaffected by preserveLayout: it governs table/list nodes,
while spreadsheet content is sheet/row/cell. There the default aligned grid is the most
faithful rendering.
[!NOTE] The synchronous
ast.toText()method was removed in v8. Use(await ast.to('text')).value, which produces the same content at its defaults and adds the configuration above.
OfficeGenerator
Use OfficeGenerator.generate(ast, format, config?) when you need to produce output from an already-parsed AST:
import { OfficeParser, OfficeGenerator } from 'officeparser';
const ast = await OfficeParser.parseOffice('report.docx');
// Convert to Markdown
const { value: md } = await OfficeGenerator.generate(ast, 'md');
// Convert to HTML with style mapping
const { value: html } = await OfficeGenerator.generate(ast, 'html', {
includeFormatting: true,
styleMap: [
{
selector: { nodeType: 'paragraph', attributes: { style: 'Heading 1' } },
output: { tag: 'h1', classes: ['main-title'] }
}
]
});
// Convert to CSV (spreadsheets)
const { value: csv } = await OfficeGenerator.generate(ast, 'csv');Supported destinations: 'text' · 'md' · 'html' · 'csv' · 'rtf' · 'pdf' · 'docx' · 'odt' · 'xlsx' · 'ods' · 'pptx' · 'odp' · 'tex' (alias 'latex') · 'epub' · 'chunks'
[!NOTE] PDF generation uses a headless browser by default (
pdfConfig.engine: 'html'), which needs the optionalpuppeteerpeer dependency:npm install puppeteerOr choose
pdfConfig.engine: 'native'to lay the document out directly withpdf-lib(npm install pdf-lib): no browser, and the only engine that produces a real PDF in the browser (import fromofficeparser/browser-native-pdffor the client-side path). To draw text the Standard-14 fonts cannot (Greek, Cyrillic, Hindi, Arabic, Chinese, ...), give it fonts andnpm install @pdf-lib/fontkit. See PdfGeneratorConfig.EPUB generation with images requires
extractAttachments: trueon the parse step that produced the AST. See EPUB Support. So do pictures and charts in PPTX and ODP output, and charts in EPUB output: a chart is written as a table of its data, which is in its attachment. Parsed without it, a chart has no data: an ODF chart (ODT, ODS, ODP) is written as its text and a DOCX or PPTX chart as its reference ([Chart: chart1.xml]) by the flowing outputs, PPTX, ODP, XLSX, ODS and CSV output report it, and an XLSX chart is not read (reported withCONTENT_PART_NOT_READ).
OfficeConverter: One-Step API
OfficeConverter.convert() combines parsing and generation in a single call. It automatically syncs parser options from the generator config: unless you set parseConfig.extractAttachments explicitly, it is enabled when the output will render images or charts, or when you enable parseConfig.ocr. An explicit parseConfig.extractAttachments (including false) always wins, and parseConfig.ocr is honored (so { parseConfig: { ocr: true } } produces OCR text through the converter, given an image-or-OCR output mode). With includeCharts: false it does not report a workbook's charts as not read (CONTENT_PART_NOT_READ): its output holds no chart.
import { OfficeConverter } from 'officeparser';
// Minimal usage
const { value: markdown } = await OfficeConverter.convert('report.docx', 'md');
// With config
const { value: html, messages } = await OfficeConverter.convert('data.xlsx', 'html', {
parseConfig: {
ignoreNotes: true,
newlineDelimiter: '\n\n',
},
generatorConfig: {
includeFormatting: true,
styleMap: [
{
selector: { attributes: { style: { value: 'Header', operator: '~=' } } },
output: { tag: 'h2', classes: ['data-header'] }
}
]
},
onWarning: (issue) => console.warn(`[${issue.code}] ${issue.message}`)
});[!IMPORTANT] The
OfficeConverterConfigshape uses nestedparseConfigandgeneratorConfigsub-objects. Do not put parser or generator options at the top level; onlyonWarninglives there. An option placed there has no effect, and is reported asUNRECOGNIZED_CONFIG_OPTIONnaming where it belongs (texConfigundergeneratorConfig,ocrunderparseConfig).
OfficeTemplate: Mail-Merge / Document Generation
OfficeTemplate.render() (alias renderTemplate) fills a Word (.docx) or OpenDocument text (.odt, .ott) template's tags from your data and returns a new document of its format (a .docx, or an .odt): {{placeholder}} fields, {{ if }} conditions that keep or drop content, and {{ for }} loops that repeat it, down to one table row per item. It is not parsing or conversion: the template is copied and only its tags change, so all of the template's formatting, layout and structure are preserved. Give it one data object for one document, or an array for a batch (one document per entry, a classic mail-merge). Think of it as a zero-dependency take on Adobe's Document Generation API.
import { OfficeTemplate } from 'officeparser';
import { writeFileSync } from 'fs';
// One document.
const bytes = await OfficeTemplate.render('invoice-template.docx', {
data: {
name: 'Acme Corp', amount: '$1,250.00', due: '2026-10-01', paid: false,
lines: [
{ description: 'Widgets', qty: 2, price: '$1,000.00' },
{ description: 'Support', qty: 1, price: '$250.00' },
],
},
});
writeFileSync('invoice-acme.docx', bytes); // Uint8Array
// A batch: one .docx per row.
const docs = await OfficeTemplate.render('invoice-template.docx', {
data: [
{ name: 'Acme Corp', amount: '$1,250.00' },
{ name: 'Globex', amount: '$980.00' },
],
});
docs.forEach((d, i) => writeFileSync(`invoice-${i}.docx`, d));Conditions and loops
Type the tags in Word or LibreOffice Writer, as ordinary text:
{{ if paid }}
Paid in full. Thank you!
{{ else if total > 1000 }}
Please pay {{ amount }} by {{ due }}. Large invoices may be paid in instalments.
{{ else }}
Please pay {{ amount }} by {{ due }}.
{{ endif }}
{{ for line in lines }}
{{ line.description }} x {{ line.qty }}: {{ line.price }}
{{ endfor }}- Where the tags sit decides what they cover. Both tags in one paragraph: the text between them. Tags in separate paragraphs: the paragraphs between them; when every tag of the block has a paragraph of its own, those paragraphs are removed with the tags, so no blank line is left (a tag beside text cuts its paragraph where it stands, and the block's other tag paragraphs then stay, empty). In an ODT, tags in items of one list: the items between them, a tag's item going with it when the tag is all it holds. Tags in different cells of one table: whole rows (in an ODT, rows inside a table's heading rows repeat there; a block cannot run from the heading rows into the body). Put
{{ for line in lines }}in a row's first cell and{{ endfor }}in its last, and the row repeats once per item; close it in a later row and that group of rows repeats.{{ if }}keeps or drops rows the same way, each branch starting a row of its own. - Conditions take a field (
{{ if paid }}) or a comparison:==,!=,<,<=,>,>=, joined withand,or,notand parentheses, against numbers, quoted strings,true,falseandnull.<>is!=too. A value can go through filters, as a field's can, before it is compared:{{ if answer | lower == "yes" }}or{{ if (a | upper) == (b | upper) }}compares without case;uppermatches more (ßandSS,ıandi) and neither is language-aware, so for one language's rules use a formatter of your own (fold: v => String(v).toLocaleLowerCase('tr')).false,null,"",0and an empty list count as false. A comparison takes two values of the same kind (two numbers, two strings, two dates) or an empty one, save that text that is a number compares with a number as that number ({{ if total > 100 }}withtotal: '250', as a form or a spreadsheet gives it; read withdataNumberSeparatorswhen set). Other text compared with a number ("150 EUR") rejects withTEMPLATE_DATA_INVALIDrather than silently picking a branch. Two texts compare as text, character by character, even when both read as numbers ('250' > '1000'is true), so'007'and'7'stay different; an order comparison of two such texts warns (TEMPLATE_NUMBERS_COMPARED_AS_TEXT, once per tag in a render or a check), andnumericTextComparison: 'number'compares them as numbers, exactly ('250' < '1000','1.0' == '1', and two 20-digit ids that differ in their last digit stay different; text too large for a double (past about 1.8e308,'1e400') is no number, as forformat, and compares as text), or'text'keeps the text comparison without the warning. Keywords are case-insensitive, and quotes may be straight or curly, as Word's AutoCorrect writes them. - Loops name their item:
{{ line.price }}in a list of objects,{{ t }}in a list of plain values. Inside a loop,loop.index(from 0),loop.number(from 1),loop.first,loop.lastandloop.lengthdescribe the current pass, so{{ for t in tags }}{{ t }}{{ if not loop.last }}, {{ endif }}{{ endfor }}writesred, blue. Loops and conditions nest. - Data can be nested.
{{ customer.address.city }}follows a path and{{ items.0.name }}reads an item of a list. A top-level key that itself holds a dot ("invoice.total") is read as typed before it is read as a path. Only the data's own keys are read, never__proto__orconstructor. - Mistakes are typed errors. A block that is never closed, a closing tag with no block, or tags that cannot enclose content together (one in a table and the other outside it, one inside a tracked change, one in a Word field and the other outside it, one in a text box, note or comment and the other outside it) reject with
TEMPLATE_SYNTAX, naming the tag and the part. A block keyword spelt as other template languages spell it ({{ elseif x }},{{ elif x }},{{ end if }}) is refused at once, with the tag as it should be typed indetails.suggestion({{ else if x }},{{ endif }}). Text that starts like a block tag but is not one ({{ If you have questions, call us }}) stays text, as prose between the delimiters always has; when the blocks around it then fail to pair, the error names it and why it could not be read ({{ if a = 1 }}: use==to compare). A value that does not fit its tag (a loop over text, a list placed as a field) rejects withTEMPLATE_DATA_INVALID. - Output Word opens. Copies of pictures, bookmarks (their names too), paragraphs and content controls get fresh ids, a footnote, endnote or comment in a copy gets a copy of its own, filled in for that pass (its fields read the pass's item), a table left with no row is removed, and a cell a branch emptied keeps an empty paragraph.
- Output LibreOffice opens. In an ODT, copies get fresh
xml:ids anddraw:ids (as does the half of a span, link or meta after a block's cut through it), note ids, and names for bookmarks, reference marks, comments, frames, tables and sections (a range's end follows its start); notes, comments and text boxes are copied with their paragraph and read the pass's item. A table or group of rows left with no row is removed, a cell, header, footer, note, text box or comment left with no paragraph keeps an empty one, and a bookmark whose start or end a branch removed is closed or dropped. The package is written as OpenDocument requires (mimetypefirst and stored); an.ottrenders to an.odt(itsmimetypeand its manifest's root media type say so), and nothing else in the manifest ormeta.xmlchanges.
Formatting values
A field can be written through filters, after |. format takes a picture, as Word's own mail merge does (its \# and \@ switches; Excel's number formats read number pictures the same way, apart from its thousands scaling, a comma after the last digit, which a picture rejects):
{{ total | format "$#,##0.00" }} $1,250.50
{{ total | format "#,##0.00 '€'" }} 1,250.50 €
{{ loss | format "$#,##0.00;($#,##0.00)" }} ($320.00)
{{ rate | format "0.0%" }} 12.5%
{{ due | format "MMMM d, yyyy" }} November 1, 2026
{{ due | format "dd/MM/yyyy HH:mm" }} 01/11/2026 09:05
{{ name | upper }} ADA LOVELACENumber pictures use
0(a digit, always written),#(a digit, written when there is one),,(groups of three),.(the decimal point) and%(a percentage); up to three sections,positive;negative;zero; text in'quotes'or after\is written as is. Values are rounded half away from zero, as Word and Excel round, and text that is a number ("1250.5", as form data carries it) is taken too. Data written with a decimal comma ("12,50","1.234,50", as German, French or Brazilian spreadsheets save it) is read withdataNumberSeparators: { decimal: ',', group: '.' }: a group separator must be followed by groups of three digits, so an ambiguous text ("1.5"with.for groups, or"0.500": a first group never starts with 0) is refused rather than misread, and a space group separator matches a no-break space too. Data written both ways (a program writing12.50, a person typing12,50) is read with a list, each way tried: text the ways read as one number is that number, and text two ways read as different numbers is refused withTEMPLATE_DATA_INVALID(reason: 'ambiguous-number', each reading indetails.readings). So[{ decimal: ',', group: '.' }, { decimal: '.' }]refuses a German whole number with one group separator, which the German way alone reads:| Text |
{ decimal: ',', group: '.' }|{ decimal: '.' }| the list of both | |---|---|---|---| |12,50| 12.5 | none | 12.5 | |12.50| none | 12.5 | 12.5 | |1.234,50| 1234.5 | none | 1234.5 | |1250| 1250 | 1250 | 1250 | |1.250| 1250 | 1.25 | refused | |1.234.567| 1234567 | none | 1234567 | |0.5| none | 0.5 | 0.5 |Date pictures use
dddddddddd(day),MMMMMMMMMM(month),yyyyyy,hhh(12-hour),HHH(24-hour),mmm(minutes),sss, andAM/PM(am/pm,A/P). As in Word,Mis the month andmthe minute: Excel'sdd/mm/yyyyisdd/MM/yyyyhere. A value is aDateor ISO 8601 text ("2026-11-01","2026-11-01T09:05:00Z"; without an offset it is UTC).The same on every machine. The formatting is officeParser's own, not the runtime's
Intl, so a template renders the same in every Node version and browser. What it writes is yours to set:numberSeparators({ decimal: ',', group: '.' }writes1.250,50; it says how numbers are written,dataNumberSeparatorshow text in the data is read),dateNames(month and day names, AM/PM), andutcOffsetMinutes(dates are written in UTC unless you move them).upperandlowerchange case, in a field or a condition; filters chain left to right ({{ code | lower | upper }}).Your own filters:
formatters: { initials: (v) => ..., round: (v, places) => ... }, used as{{ name | initials }}or{{ x | round 2 }}. Each gets the value as the data holds it (a list or an object too) and the strings and numbers written after its name.A picture given the wrong kind of value (a date picture on a number, a number picture on text that is no number) rejects with
TEMPLATE_DATA_INVALID; a picture that cannot be read rejects withTEMPLATE_SYNTAX. A word after|that is no filter leaves the whole tag as text, in a field or a condition, like other prose between the delimiters. An empty value stays empty, and a missing one followsonMissing.
Fields
- Run-aware. Word often splits a typed
{{name}}across several runs ({{,na,me}}), and LibreOffice across spans; it is filled anyway, and a value takes the formatting of the run its placeholder sat in (a bold{{amount}}renders bold). Block tags are found the same way, and in an ODT a tag holding spaces kept as<text:s/>reads as typed (a long run is read as 16 spaces, or more than twice the longest run of spaces in a delimiter, and written back whole). - Everywhere text lives. Tags in the body, headers, footers, footnotes/endnotes, comments and text boxes are all filled (in an ODT, headers and footers are in
styles.xml, the body incontent.xml). Values may contain\n(rendered as line breaks; in an ODT, tabs and runs of spaces are kept too). Deleted text kept for tracked changes is not read. - Field names are Unicode letters and digits plus
_,.,-(e.g.{{invoice.total}},{{customer-name}}).if,else,endif,forandendforare the block tags' own words. Other text between the delimiters, such as prose with spaces, is left as literal text, so it is never mistaken for a field. - Deterministic output (pinned zip timestamps): the same template + data always renders byte-identical bytes.
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| data | TemplateData \| TemplateData[] | (required) | Field values, which may be nested objects and lists. One object is one document; an array is one document per entry |
| delimiters | { start: string; end: string } | {{ }} | Tag delimiters, for fields and block tags alike |
| numberSeparators | { decimal?, group? } | { decimal: '.', group: ',' } | What a number picture writes for its point and groups |
| dataNumberSeparators | { decimal?, group? } \| { decimal?, group? }[] | (none: . for the point, no groups) | How numbers written as text in the data are read, by a number picture and where a condition compares them with a number: { decimal: ',', group: '.' } reads 1.234,50. Each one character; group may be ''. A list of one to 8 is tried in turn; text two ways read differently is refused |
| numericTextComparison | 'text' \| 'number' | (none: as text, a warning on < <= > >=) | How a condition compares two texts that both read as numbers: as text, or as numbers, exactly (text too large for a double is no number) |
| dateNames | TemplateDateNames | English | Month and day names (full and short) and AM/PM markers a date picture writes |
| utcOffsetMinutes | number | 0 | Minutes added to UTC before a date picture writes a date (330 for India); a fixed offset, the same on every machine |
| formatters | Record<string, TemplateFormatter> | (none) | Filters of your own, used after \| in a tag |
| onMissing | 'keep' \| 'empty' \| 'error' | 'keep' | A field the data does not have: leave it as typed (a block whose condition or list uses it is left as typed, tags included), treat it as empty (blank text, a false condition, a loop of nothing), or reject with TEMPLATE_FIELD_MISSING. A field present but null/undefined always renders empty |
| password | string | (none) | Decrypt the template first, if it is itself password-protected |
| onPassword | (reason) => string \| undefined \| Promise<...> | (none) | Supplies a password when password did not decrypt the template, as the parser's onPassword does |
| maxPasswordAttempts | number | 3 | How many times onPassword is asked before the render rejects |
| decompressionLimits | DecompressionLimits | parser's | Bounds unzipping an untrusted template; maxXmlElements (2,000,000) bounds its elements, its tags and the tokens they hold together (XML_ELEMENT_LIMIT_EXCEEDED), and maxRepeatedContent (16 MiB) what loops and repeated values add, past which the render rejects with OUTPUT_TOO_LARGE |
Listing tags and checking data
OfficeTemplate.tags() lists a template's tags without any data, to build a form or a data source from it. OfficeTemplate.check() reads your data against the template without writing a document, and returns every problem a render with onMissing: 'error' would reject for, all at once instead of the first.
// What the template reads: each field, and each tag opening a block or branch, in the order of each part
// (and text that looks like a tag but is not one, as kind 'text').
const tags = await OfficeTemplate.tags('invoice-template.docx');
// [{ kind: 'field', tag: '{{name}}', part: 'word/document.xml', fields: ['name'], loops: [] },
// { kind: 'for', tag: '{{ for line in lines }}', part: 'word/document.xml', fields: ['lines'], item: 'line', loops: [] },
// { kind: 'field', tag: '{{ line.qty }}', part: 'word/document.xml', fields: ['line.qty'], loops: [{ item: 'line', list: 'lines' }] }, ...]
// Check before rendering.
const data = { name: 'Acme Corp', lines: [{ description: 'Widgets', qty: 2 }, { description: 'Support' }] };
const problems = await OfficeTemplate.check('invoice-template.docx', { data });
// [{ code: 'TEMPLATE_FIELD_MISSING', field: 'line.qty', part: 'word/document.xml',
// loops: [{ list: 'lines', index: 1 }], message: "Template field 'line.qty' has no matching value in the data, ..." }, ...]
if (problems.length === 0) await OfficeTemplate.render('invoice-template.docx', { data });- A tag (
TemplateTagInfo) has itskind(field,if,elseif,elseorfor;endifandendforare not listed), the tag as typed, thepartholding it (a header's tags are inword/header1.xml, or an ODT's instyles.xml), the paths it reads (fields), a field'sfilters, or the filters anif's condition uses, a loop'sitem, and the loops it is inside, outermost first. - Text that looks like a tag but is not one is listed among the tags as
kind: 'text', with the text as typed, its part, its loops, no fields, and why inreason, andrender()andcheck()report it once with aTEMPLATE_NOT_A_TAGwarning (details.texts, anddetails.found, how many there are); a render writes it as typed. At most 1,000 such texts are listed for a template, intags()and in the warning: past them they are only counted, the warning's message says how many were found, andtags()reports the warning when its list was cut. Exactly four shapes count: a name with spaces between the delimiters ({{first name}},space-in-name: letters, digits,_ . -and spaces, starting with a letter or_, at most 64 characters), a word after|that is no filter ({{ total | fromat "0.00" }},unknown-filter), a delimiter a character short around a name ({{ name },{ name }},missing-delimiter, for delimiters of one character typed twice or more), and delimiters typed full-width around a name ({{nom}},full-width). Single braces,{x},{ x }, JSON, code that nests braces ({a: {b}},{{x}, {y}}: a short delimiter is reported only where the braces around it do not balance) and an empty{{}}are never reported. Braces are counted only when the start and the end are different characters: with delimiters such as$$and$$, every delimiter a character short around a name is reported. - A problem (
TemplateProblem) isTEMPLATE_FIELD_MISSING(with thefield) orTEMPLATE_DATA_INVALID(withdetails: why, the tag and the kinds of value), with the render'smessage, thepart, and the loops it was read in, each with its pass from 0. - A check follows the data as a render does: the branch each condition picks (a field only another branch uses is not needed), every pass of every loop, and nothing inside a list that is missing or does not fit, whose fields are checked once it is given. A key present but
undefinedornullcounts as given, as a render writes it empty. - A batch (
dataas an array) returns one list per entry. A check lists at most 1,000 problems for one entry and 10,000 for a batch, and says it stopped with aTEMPLATE_CHECK_LIMITEDwarning. A template's own errors (TEMPLATE_SYNTAX) and the limits a render is held to reject as a render's do.
Word (.docx) and OpenDocument text (.odt, .ott) templates are supported, the format read from the bytes (fileType only words the error); another format (a spreadsheet, a presentation) rejects with TEMPLATE_UNSUPPORTED_FORMAT. An encrypted template is decrypted with password or onPassword, and the document written is plain (an ODT's manifest loses its encryption data).
Native RAG Chunking
officeParser provides native document chunking for Retrieval-Augmented Generation (RAG) pipelines with three strategies:
Strategy 1: Document Structure (Default)
Splits at natural AST boundaries (paragraphs, headings, pages, slides, sheets). Preserves logical flow.
const { value: chunks } = await OfficeConverter.convert('report.docx', 'chunks', {
generatorConfig: {
chunksConfig: {
strategy: 'document-structure',
splitBy: 'heading', // 'paragraph' | 'heading' | 'page' | 'slide' | 'sheet'
maxChunkSize: 1500,
tableSplitStrategy: 'row', // repeats header row in every chunk, ideal for RAG
}
}
});Strategy 2: Fixed-Size (Recursive)
Splits by character count with overlap. Equivalent to LangChain's RecursiveCharacterTextSplitter. UTF-16 surrogate pairs stay whole: overlap may expand to include a complete code point, and a hard-cut chunk may exceed the size limit if one code point alone does not fit.
const { value: chunks } = await OfficeConverter.convert('report.docx', 'chunks', {
generatorConfig: {
chunksConfig: {
strategy: 'fixed-size',
chunkSize: 1000,
chunkOverlap: 200,
}
}
});
console.log(`Generated ${chunks.length} chunks`);Strategy 3: Semantic
Uses cosine similarity between sentence embeddings to find topic boundaries. Requires you to provide an embeddingFunction.
import OpenAI from 'openai';
const openai = new OpenAI();
const { value: chunks } = await OfficeConverter.convert('report.docx', 'chunks', {
generatorConfig: {
chunksConfig: {
strategy: 'semantic',
embeddingFunction: async (text) => {
const res = await openai.embeddings.create({
input: text, model: 'text-embedding-3-small'
});
return res.data[0].embedding;
},
similarityThreshold: 0.8,
maxChunkSize: 2000,
}
}
});The OfficeChunk Object
generate(ast, 'chunks') (and ast.to('chunks')) resolves to a real OfficeChunk[] array, not a JSON string - serialize it to JSON/JSONL yourself if your pipeline needs that.
Every chunk contains text and rich metadata for citations and filtered retrieval:
interface OfficeChunk {
text: string;
/** Rich metadata for filtered retrieval */
metadata: {
sourceType: string; // e.g., 'docx', 'pdf'
pageNumber?: number; // (PDF only)
slideNumber?: number; // (PPTX only)
sheetName?: string; // (XLSX only)
closestHeading?: string; // Nearest heading above this chunk
isTableChunk?: boolean; // True if part of a split table
};
startIndex?: number; // Character offset (if addStartIndex: true)
endIndex?: number; // End character offset (if addStartIndex: true)
}The AST Structure
OfficeParserAST is a format-agnostic document representation:
OfficeParserAST
├── type: 'docx' | 'pdf' | 'xlsx' | 'csv' | 'md' | 'epub' | 'tex' | ... (14 formats)
├── metadata: { author, title, created, modified, keywords, customProperties, nativeProperties, styleMap, pageSetup, ... }
├── content: [ OfficeContentNode ]
│ ├── type: 'paragraph' | 'heading' | 'table' | 'list' | 'image' | 'chart' | 'drawing' | 'comment' | 'admonition' | 'embed' | 'definitionList' | ...
│ ├── text: string (concatenated text of node + all descendants)
│ ├── children: [ OfficeContentNode ] (recursive structural children)
│ ├── notes: [ OfficeContentNode ] (footnotes/endnotes/slide notes attached to this node; see below)
│ ├── comments: [ OfficeContentNode ] (inline comments attached to this node)
│ ├── formatting: { bold, italic, underline, color, size, font, alignment, ... }
│ ├── bounds?: { x, y, width, height, baseline? } (place on its page or slide: PDF, PPTX, ODP; baseline: a PDF text run's)
│ └── metadata: { level, listId, marker, row, col, rowSpan, colSpan, backgroundColor, spacing, style, ... }
├── auxiliary?: OfficeAuxiliaryContent (out-of-band layout elements)
│ ├── headers?: OfficeContentNode[] ('header' nodes: DOCX parts, PDF top band (tagged artifacts, or lines repeated across an untagged PDF's pages), ODT master pages, LaTeX fancyhdr; metadata { type, section, hidden })
│ ├── footers?: OfficeContentNode[] ('footer' nodes, likewise)
│ ├── slideMasters?: OfficeContentNode[] (PPTX slide masters)
│ └── outline?: OfficeContentNode[] (PDF bookmark outline)
├── attachments: [ OfficeAttachment ] (populated when extractAttachments: true)
│ ├── type: 'image' | 'chart'
│ ├── name: string
│ ├── mimeType: string
│ ├── data: string (Base64)
│ ├── ocrText?: string (if ocr: true AND extractAttachments: true)
│ └── chartData?: { title, dataSets, labels, ... } (see Charts below)
├── warnings: OfficeIssue[] (non-fatal issues from the parsing phase)
├── config: OfficeParserConfig (the resolved parse config; `.to()` inherits newlineDelimiter/onWarning from it)
└── to(format, config?) (format: 'html'|'md'|'text'|'csv'|'rtf'|'pdf'|'docx'|'odt'|'xlsx'|'ods'|'pptx'|'odp'|'tex'|'epub'|'chunks', returns { value, messages })A note or comment referred to from several places is one node that each reference's notes (or comments) array holds. Test for a note you have already seen by identity before handling it again, and do not mutate it expecting only one reference to change. (JSON.stringify writes it at every reference.)
OfficeIssue: Warning / Error Object
All warnings and errors (from both parsing and generation) use this shape:
interface OfficeIssue {
type: 'warning' | 'info' | 'error';
code: OfficeWarningType | OfficeErrorType; // typed enum, e.g. 'OCR_FAILED'
message: string;
node?: OfficeContentNode; // the node that triggered the issue, if any
details?: any; // original error or extra context
page?: number; // the PDF page it is on (1-based), where the reader knows it
}Thrown errors carry the same object on error.officeIssue, so a failed parse is identified by
the same stable code you would branch on for a warning, rather than by matching message text:
try {
const ast = await officeParser.parseOffice(buffer, { fileType: 'docx' });
} catch (err) {
switch (err.officeIssue?.code) {
case 'ZIP_NO_ENTRIES_FOUND': // not a ZIP archive at all
case 'ZIP_TRUNCATED': // cut off in transfer, entries incomplete
case 'REQUIRED_PART_MISSING': // readable ZIP, but not the format it claims
console.error('Unusable file:', err.officeIssue.message);
break;
default:
throw err;
}
}[!IMPORTANT] A corrupt file throws; it does not parse as an empty document. If an archive is not readable, is truncated, or is missing the part its format requires (
word/document.xml,xl/workbook.xml,ppt/presentation.xml, ODFcontent.xml, the EPUB OPF), parsing rejects with one of the codes above. An empty result therefore means the document really is empty. Files that are legitimately empty still parse, and say so throughonWarning/ast.warnings(NO_WORKSHEETS_FOUNDfor a chartsheet-only workbook,NO_SLIDES_FOUNDfor a presentation with no slides).
Warning codes (type: 'warning' | 'info', delivered to onWarning and collected in ast.warnings)
These never throw; they report a degraded-but-successful outcome you may branch on by code.
| Code | Phase | Meaning / what to do |
|---|---|---|
| OCR_REQUIRES_ATTACHMENTS | parse | ocr: true without extractAttachments: true; no OCR ran. Set both. |
| PDF_NO_TEXT_EXTRACTED | parse | A PDF yielded little or no text (likely scanned). Set ocr: true + extractAttachments: true. The message says how many characters were read, if any. Where a CMap its fonts need could not be loaded (PDF_ASSET_UNAVAILABLE), or the text is in fonts with no character map, it also says so and names them: details is then { pages, cMaps?, fonts? }, otherwise the page count. |
| PDF_ASSET_UNAVAILABLE | parse | A file pdf.js needed could not be had: a CMap (text in the fonts using it is missing) or an image decoder (pictures needing it are not decoded). Once per file per document, with the first page that needed it (page); the message names the file, why (missing, failed, or blocked by allowRemoteAssets: false) and the option to set (pdfCMapUrl or pdfDecoderUrl). |
| PDF_STRUCT_TREE_UNRELIABLE | parse | PDF tag tree absent/incomplete; structure recovered geometrically. |
| PDF_TEXT_ENCODING_SUSPECT | parse | A fifth or more of a PDF's characters (of at least 50) are unmappable glyphs (broken ToUnicode); text may be garbage. Consider OCR. |
| PDF_FONT_UNMAPPED | parse | A font's text is likely its glyph ids read as characters (no /ToUnicode on an Identity encoding, control characters among its text). Once per font name, with the first page (page), details.font and every page in details.pages. Consider OCR. |
| PDF_OUTLINE_TRUNCATED | parse | Bookmark outline hit the depth/size cap; ast.auxiliary.outline is partial. |
| PDF_WORKER_MISSING / PDF_WORKER_FALLBACK | parse | The pdf.js worker could not be loaded / a fallback was used (set pdfWorkerSrc). |
| REMOTE_ASSET_BLOCKED | parse (error) | allowRemoteAssets is false and the parse would have fetched the pdf.js worker or Tesseract's files from the library's default CDN address. The message names the options to set (pdfWorkerSrc, ocrConfig.workerPath/corePath/langPath). |
| NO_WORKSHEETS_FOUND / NO_SLIDES_FOUND | parse | A legitimately
