Document loader for PowerPoint (.pptx), Excel (.xlsx), OpenDocument (.odt, .ods, .odp), RTF (.rtf) and EPUB (.epub) files, read in memory through officeparser, which is imported on the first read.

The loader reads bytes under the format its caller names: it reads no file from a path and does not guess a format from the bytes. The text is the document's as officeparser writes it, without its comments and without its pictures.

With OfficeLoaderOptions.ocrPages above 0, load(bytes, { format: 'pdf' }) reads a scanned PDF in a worker thread of its own (loadIsolated's kind 'scan'): officeparser reads the first ocrPages pages with pdf.js, in a process of its own capped at pdfProcessMemoryMb, and tesseract.js recognizes the text of each picture on them. A picture OCR could not read is left out of the text, and the read goes on. A failure tesseract.js throws outside officeparser's promise ends the worker thread, and the read rejects with it.

Implements

Example: An uploaded deck, refused past its bounds

const loader = new OfficeLoader({ maxInflatedBytes: 32 * 1024 * 1024, maxXmlElements: 400_000 });
try {
const doc = await loader.load(uploadedBuffer, { format: 'pptx', signal: AbortSignal.timeout(30_000) });
console.log(doc.metadata.wordCount);
} catch (error) {
if (error instanceof DocumentTooLargeError) {
console.log(error.cause); // names the bound the document reached
}
}

Example: A scanned PDF, its first three pages read by OCR

const loader = new OfficeLoader({ ocrPages: 3 });
const doc = await loader.load(scanBuffer, { format: 'pdf', signal: AbortSignal.timeout(120_000) });

Implements

Constructors

Methods

Properties

Constructors

Methods

  • True for a file name with one of the loader's extensions. A buffer does not say its format, so the answer for one is false; load reads it under the format its caller names.

    Parameters

    • source: string | Buffer<ArrayBufferLike>

      A file name or path, or a document's bytes.

    Returns boolean

  • Reads a document's bytes under the format its caller names.

    Parameters

    • source: string | Buffer<ArrayBufferLike>

      The file's bytes. A path is refused: the loader reads no file.

    • Optional options: OfficeLoadOptions

      format names the document's format: a value of OFFICE_TYPES, with or without a leading dot, or pdf for a scanned PDF. signal cancels this read.

    Returns Promise<LoadedDocument>

    The document's text, with metadata.wordCount, and metadata.pageCount when officeparser reports a page count.

    Throws

    When the document reaches a bound, one officeparser only warns of included. The error's limit is the loader's maxInflatedBytes whichever bound was reached, and its cause names the bound by officeparser's code. So does its message, but past maxInflatedBytes, where the message gives that bound in bytes.

    Throws

    For a PDF while ocrPages is 0, and for a scan whose pages gave no text. When officeparser reported a picture OCR could not read, the first such failure is the error's cause.

    Throws

    The reason of options.signal when the caller aborts it.

    Throws

    When source is a path, when options.format names none of the formats, when officeparser is not installed, and when a scan's pdf.js would run in the reading thread; the error a scan's worker thread ended on, and loadIsolated's refusals of a scan's worker; and officeparser's own error for a file it cannot read.

Properties

supportedExtensions: string[] = ...

File extensions this loader handles, each with a leading dot.

Used by LoaderRegistry to route file paths to the correct loader.

Example

['.md', '.mdx']