docx

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package docx extracts body text from DOCX (Office Open XML) sources.

A .docx is a ZIP archive of XML; the document body lives at word/document.xml. This extractor unzips in memory, stream-decodes that part, and concatenates text runs (<w:t>), emitting a blank line between paragraphs (<w:p>) so the downstream boundary-aware chunker sees paragraph boundaries. The result is a single text/plain artifact in the same shape as the core text extractor.

Unlike the PDF and HTML extractors, this package needs no third-party dependency (stdlib archive/zip + encoding/xml). It is kept a subpackage anyway so all formats are uniformly opt-in (see docs/adr/0006-optional-adapters-as-subpackages.md). Wire it into a registry with:

reg.Register("application/vnd.openxmlformats-officedocument.wordprocessingml.document", docx.New())

Scope is body text only: headers, footers, footnotes, endnotes, comments, and embedded objects are not extracted. That can be added later by parsing the additional archive parts; it is omitted now to keep the first version small.

Index

Constants

View Source
const DefaultMaxDecompressedBytes int64 = 64 << 20 // 64 MiB

DefaultMaxDecompressedBytes caps how much of word/document.xml the decoder will read. The Registry bounds the *compressed* input, but a zip bomb can expand far beyond that, so the uncompressed stream needs its own ceiling. 64 MiB is far larger than any realistic body-text document.xml while still bounding memory.

View Source
const MediaType content.MediaType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

MediaType is the media type this extractor handles.

Variables

This section is empty.

Functions

This section is empty.

Types

type Extractor

type Extractor struct {
	// MaxDecompressedBytes caps the uncompressed size of word/document.xml the
	// decoder reads, defending against a zip bomb whose compressed form fits under
	// the Registry's input cap but expands to exhaust memory. Zero or negative
	// means DefaultMaxDecompressedBytes.
	MaxDecompressedBytes int64
}

Extractor extracts body text from DOCX sources. It carries only immutable configuration and is safe for concurrent use.

func New

func New() *Extractor

New returns a DOCX Extractor using DefaultMaxDecompressedBytes.

func (Extractor) Extract

func (e Extractor) Extract(ctx context.Context, r io.Reader, parentID string) ([]content.Artifact, error)

Extract reads r as a .docx archive and returns a single text/plain artifact derived from parentID, with paragraphs separated by blank lines. It honors ctx cancellation while reading. It returns:

  • extract.ErrNoContent if the document parses cleanly but has no body text;
  • an error matching extract.ErrMalformedSource if the archive is not a valid zip, lacks word/document.xml, contains unparseable XML, or whose word/document.xml decompresses past MaxDecompressedBytes (a zip bomb).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL