Skip to main content

Crate rwml

Crate rwml 

Source
Expand description

rwml — one native Rust reader for both Microsoft Word formats: legacy .doc (Word 97–2003 binary, [MS-DOC]) and modern .docx (OOXML WordprocessingML). No JVM, no Apache POI, no external .docx crate, no shelling out — Document::open format-detects from the magic bytes and both feed the same DocModel and Markdown/HTML exporters.

  • .doc is an OLE2/CFB compound file. The text lives in the WordDocument stream; the piece table (CLX) in the 0Table/1Table stream maps character positions to byte offsets, and each piece is either UTF-16LE (Korean body text) or 8-bit text in the document’s ANSI codepage (fCompressed — cp1252 for Western, cp949 for Korean, from the FIB language id).
  • .docx is a ZIP of XML parts (word/document.xml + styles, numbering, relationships, media), parsed with zip + quick-xml behind the default docx feature. Disable it (default-features = false) for a dependency-light .doc-only build.
// Works for either format — detection is automatic.
let bytes = std::fs::read("report.doc").unwrap();
let text = rwml::extract_text(&bytes).unwrap();
println!("{text}");

Two surfaces:

  • Flat textextract_text / Document::text, the same output as POI WordExtractor.getText() (fast, allocation-light).
  • A full document modelDocument::model (paragraphs, character runs with bold/italic/…, structured tables with colspan/rowspan, headings, lists, hyperlinks, and extracted images), plus Document::to_markdown and Document::to_html. Built lazily, so the flat path never pays for it.

Parsing untrusted input is panic-free / bounds-checked: a malformed or hostile .doc/.docx yields Error, never a crash. (The only expect is on the crate’s own compiled-in blank template behind the infallible Document::new/Default; use Document::try_new for a Result instead of that build-invariant panic.)


rwml (from WordprocessingML) is an independent project, not affiliated with or endorsed by Microsoft. Microsoft Word and the .doc/.docx formats are Microsoft trademarks, referenced only to indicate format compatibility; the crate is built solely from the public [MS-DOC]/[MS-CFB]/OOXML specifications.

Modules§

wasm
Browser/WASM adapter functions over the same core crate::Document API.

Structs§

AuthoredComment
A comment to author on a generated run.
AuthoredContentControl
Plain text content-control metadata to author on a generated run.
AuthoredNote
Footnote or endnote metadata to author after a generated run.
AuthoredRevision
Tracked revision metadata to author on a generated run.
Cell
A table cell — may hold block content and span rows/columns.
CellBuilder
Thin builder for a table cell with block content and layout metadata.
CellMargins
Table-cell margins in twips (1/20 point), ordered by physical side.
CharProps
Character-level formatting that affects rendering.
Chart
A block-level chart with literal category and numeric caches.
ChartBuilder
Thin builder for a block-level Chart.
ChartSeries
One named chart series with literal numeric values.
Color
An sRGB color.
Comment
A Word comment extracted from a .docx comments part or recovered from a legacy .doc annotation subdocument.
CommentBuilder
Thin builder for a generated Word comment body and metadata.
ContentControlBuilder
Thin builder for generated plain text content-control metadata.
CoreProperties
Core document properties extracted from docProps/core.xml or generated document setup metadata.
DocBuilder
Thin builder for creating a DocModel without filling every struct field by hand.
DocGrid
Document grid settings for a WordprocessingML section.
DocMeta
Document-level metadata.
DocModel
A whole .doc document as an ordered list of block-level nodes plus document-level metadata.
DocSetup
Document-level layout + metadata, for authoring and rendering. All fields are optional/default so existing read paths are unaffected.
Document
A parsed Word document — either legacy .doc (OLE2/[MS-DOC]) or modern .docx (OOXML). Document::open format-detects from the magic bytes and both backends feed the same DocModel and exporters, so text(), to_markdown(), to_html(), and images() behave identically regardless of which Word format the bytes are in.
DocumentReport
Summary of the opened document’s format, visible stats, observed feature markers, and warnings.
EditCapability
Whether package-preserving edit APIs are available for an opened document.
FeatureInventory
Counts of Word features observed while opening a document.
Field
A Word field observed in a .docx body.
FieldContext
Caller-supplied evaluation context for volatile or external-data fields.
FieldEvaluationReasonCount
Count of unsupported field evaluations for one reason.
FieldKindCount
Count of observed fields for one field kind.
FloatingShape
Floating shape geometry recovered from a Word document.
HeaderFooter
A running header/footer record recovered from a Word document.
Image
An embedded image. bytes/mime are present only when the picture was extracted; otherwise the node is a placeholder.
ImageBuilder
Thin builder for an embedded Image block.
Indent
Paragraph indentation in points; None = unset.
ListInfo
List membership of a paragraph.
MetafileInfo
Best-effort metadata for a preserved WMF/EMF package part.
Note
A footnote or endnote recovered from a Word document.
PageSetup
Page geometry, in points. Default is A4 portrait with 1-inch margins.
ParaProps
Paragraph-level properties.
Paragraph
A paragraph: inline runs plus paragraph-level properties.
ParagraphBuilder
Thin builder for a paragraph with runs and paragraph-level layout.
ParagraphStyle
A paragraph style definition for generated .docx output.
ParagraphStyleBuilder
Thin builder for a paragraph style definition.
RenderReport
Renderer metrics and warnings for a generated PDF.
RenderedPdf
PDF bytes plus the render report produced by the same pagination pass.
Revision
A tracked revision extracted from a .docx body.
RevisionBuilder
Thin builder for generated tracked insertion/deletion metadata.
Row
A table row.
Run
An inline run of text with uniform character properties.
RunBuilder
Thin builder for an inline Run with character formatting.
SectionSetup
Section-level layout recovered from or generated into .docx section properties.
ShapeDistance
Distance from a floating shape to surrounding text, in English Metric Units.
ShapeEffectExtent
Visual-effect extents for a recovered floating shape, in English Metric Units.
ShapeExtent
Drawing extent for a recovered floating shape, in English Metric Units.
ShapePoint
Absolute point for a recovered floating shape, in English Metric Units.
ShapePosition
Positioning metadata for a recovered floating shape.
ShapeWrapping
Text-wrapping policy declared by a floating shape anchor.
SourceRegion
A span of DocModel::blocks that came from one source subdocument.
Spacing
Paragraph spacing in points; None = unset (renderer/writer uses defaults).
Stats
Aggregate document statistics, mirroring the project-wide DocStats contract.
Table
A table: rows of cells.
TableBorderColors
Optional table border colors by physical side.
TableBorderSizes
Optional table border widths by physical side, in eighths of a point.
TableBorderStyles
Optional table border line styles by physical side.
TableBuilder
Thin builder for rich Table values.
TextAnchor
A text range in the main document body associated with an annotation.
TextBox
A text box recovered from a Word document.
WebExtensionTaskPane
A generated Office web-extension task pane package entry.

Enums§

Align
Paragraph alignment.
Block
A block-level node.
ChartKind
Supported chart layouts for authored .docx output.
ChartShape
Supported shape styles for authored 3-D bar/column charts.
CoreProperty
Editable .docx core document properties supported by Document::set_core_property.
DocGridType
Document grid behavior for a WordprocessingML section.
DocumentFormat
Source document format detected by crate::Document::open.
DocumentWarning
Human- and machine-readable warnings derived from FeatureInventory.
EditReadOnlyReason
Reason package-preserving edits are unavailable for an opened document.
Error
Errors produced while opening or decoding a .doc file.
FieldEvaluationReason
Reason a field’s computed value is not available.
FieldKind
Known Word field instruction classes rwml distinguishes.
FieldRole
Whether a run is plain text or the cached result of a field.
FieldUnsupportedReason
Best-effort reason an imported field remains cached in the model.
HeaderFooterKind
Kind of running header/footer region recovered from a Word document.
MetafileFormat
Metafile image container observed in a package.
NoteKind
Kind of note recovered from a Word document.
PageNumberFormat
Display format for generated section page numbers.
RenderWarning
Human- and machine-readable warnings derived from features the current renderer cannot faithfully compute or draw yet.
RevisionKind
Kind of tracked revision marker observed in a .docx body.
RevisionView
Flat text view policy for tracked revisions.
SectionBreakKind
Section-break start behavior for a WordprocessingML section boundary.
SourceRegionKind
A coarse source subdocument region from the original Word file.
TableBorderSide
A physical side of a table border.
TableBorderStyle
Uniform table border line style.
TextDirection
Text flow direction for generated .docx section properties.
VCell
Vertical alignment of cell content.
VertAlign
Vertical alignment of a run (super/subscript).

Functions§

extract_text
Convenience: decode .doc bytes into normalized plain text (all sub-documents — main body, then footnotes/endnotes/headers). Errors with Error::NoText if nothing indexable was found.
try_write_docx
Fallible variant of write_docx: returns the serialization error instead of an empty buffer if packaging ever fails. Prefer this when you need to surface write failures rather than silently emit nothing.
write_docx
Serialize a DocModel — one you built from data, built with DocBuilder, or read from a .doc/.docx — to a clean, Office-openable .docx byte buffer. This is the authoring entry point: construct a model (paragraphs/runs with fonts, sizes, colors; headings; styled/sized/shaded tables; images; page setup) and write a styled Word document. Available with the default docx feature.

Type Aliases§

Result
Convenience alias.