WordReader
Read .docx files: content, styles, tables, images, layout, and rebuild a document with toWriter().
Introduction to WordReader#
WordReader (docxlib.ring) parses .docx files and provides:
- Structured access to the document content as Ring lists
- Semantic query methods —
getHeadings(),getTables(),getSectionLayouts(), etc. - Full round-trip —
toWriter()converts the parsed document back into aWordWriterthat you can modify and re-save
load "docxlib.ring"
reader = new WordReader("report.docx")
reader.loadDocx()
? reader.summary()
headings = reader.getHeadings()
for h in headings
? string(h[:level]) + ": " + h[:text]
next
reader.save("report_copy.docx")
reader.cleanup()Loading and Saving#
| Method | Parameters | Description |
|---|---|---|
new WordReader(filePath) | filePath : String | Constructor — sets the source .docx path |
loadDocx() | — | Unzip and parse the document. Call once after construction |
save(outputPath) | outputPath : String | Round-trip: parse → reconstruct → write |
cleanup() | — | Delete the temporary extraction folder |
toWriter() | — | Return a populated WordWriter from the parsed content |
Note
loadDocx() resets all internal state each call, so one WordReader instance can be reused across multiple files.
Document Properties#
| Method | Returns | Description |
|---|---|---|
getFilePath() | String | Source file path |
getTitle() | String | Document title from core.xml |
getAuthor() | String | Document author |
getDefaultFont() | String | Body font name |
getDefaultFontSize() | Number | Body font size in points |
getPageWidthCm() | Number | Page width in cm |
getPageHeightCm() | Number | Page height in cm |
getMarginTopCm() | Number | Top margin in cm |
getMarginBottomCm() | Number | Bottom margin in cm |
getMarginLeftCm() | Number | Left margin in cm |
getMarginRightCm() | Number | Right margin in cm |
getOrientation() | String | "portrait" or "landscape" |
getHeaderText() | String | Default header text |
getFooterText() | String | Default footer text |
getEvenPageHeaderText() | String | Even-page header |
getEvenPageFooterText() | String | Even-page footer |
getFirstPageHeaderText() | String | First-page header |
getFirstPageFooterText() | String | First-page footer |
hasEvenAndOddHeaders() | Bool | Even/odd header mode enabled |
hasFirstPageDifferent() | Bool | First-page-different mode enabled |
hasPageBorder() | Bool | Page border present |
getPageBorderStyle() | String | Page border style |
getPageBorderColor() | String | Page border colour hex |
getPageBorderSizePt() | Number | Page border width in points |
getPageBgColor() | String | Page background colour hex |
hasPageBackground() | Bool | Solid page background present |
getColumnCount() | Number | Number of columns in the document |
getColumnSpaceCm() | Number | Column gap in cm |
getTOCTitle() | String | Table of contents title |
hasTOC() | Bool | TOC field present |
hasWatermark() | Bool | Text watermark present |
getWatermarkText() | String | Watermark text |
getWatermarkOptions() | List | Watermark properties |
isDocumentRTL() | Bool | Document-wide RTL enabled |
hasPageNumbers() | Bool | Page number field present |
getPageNumberAlign() | String | Page number alignment |
getCustomProperties() | List | All custom document properties |
getCustomProperty(name) | String | A single custom property by name |
Content Query Methods#
Summary and Block Listing#
? reader.summary() # human-readable content overview
reader.listBlocks() # numbered block list with type + preview
blocks = reader.getBlocks() # raw list of all parsed blocksParagraphs and Runs#
| Method | Returns | Description |
|---|---|---|
getHeadings() | List of [:level, :text, :style] | All heading blocks (levels 1–6) |
getParagraphs() | List of paragraph blocks | All body paragraphs with formatting |
getRTLParagraphs() | List | Right-to-left paragraphs |
getCommentedParagraphs() | List of [:text, :commentText, :commentAuthor] | Paragraphs with attached comments |
getKeepNextParagraphs() | List | Paragraphs with keep-with-next set |
getPageBreakBeforeParagraphs() | List | Paragraphs preceded by a page break |
getBorderedParagraphs() | List | Paragraphs with a border box |
getNamedStyleParagraphs(name) | List | Paragraphs using a specific named paragraph style |
getOutlinedParagraphs() | List | Paragraphs with an explicit outline level |
getNumberedHeadings() | List | Headings that are part of a numbered list |
getTabbedParagraphs() | List | Paragraphs containing tab characters |
getHiddenRuns() | List of [:text] | Runs with vanish (hidden text) |
getCapsRuns() | List of [:text, :type] | Runs with allCaps or smallCaps |
getDStrikeRuns() | List of [:text] | Runs with double-strikethrough |
getRunBorderRuns() | List | Runs with a character-level border |
getRunLanguages() | List of [:lang, :text] | Runs with explicit language tags |
getCharStyleRuns() | List of [:styleName, :text] | Runs referencing a named character style |
getCharStyles() | List | All character style names found in the document |
getTextBoxes() | List of [:text] | Floating text box content |
Lists, Hyperlinks, Captions, Bookmarks#
| Method | Returns | Description |
|---|---|---|
getListItems() | List of [:text, :level, :isBullet] | All list items with nesting level |
getListRestartPoints() | List | List items where numbering restarts |
getHyperlinks() | List of [:text, :url] | All external hyperlinks |
getCaptions() | List of [:text, :label, :seqNum] | SEQ figure/table captions |
getBookmarks() | List of [:name, :text] | Named bookmark anchors |
Footnotes and Endnotes#
| Method | Returns | Description |
|---|---|---|
getFootnotes() | List of note blocks | All footnotes |
getEndnotes() | List of note blocks | All endnotes |
Each note block contains:
block[:id] — note reference number
block[:refText] — anchor text in the document body
block[:noteText] — plain concatenated note body text
block[:runs] — list of formatted runs, each: [:text, :bold, :italic,
:underline, :color, :font, :size]footnotes = reader.getFootnotes()
for fn in footnotes
? "Note " + fn[:id] + ": " + fn[:noteText]
for r in fn[:runs]
if r[:bold] = true ? " BOLD: " + r[:text] ok
next
nextComments, Fields, Charts#
| Method | Returns | Description |
|---|---|---|
getComments() | List of [:id, :text, :author] | All comment entries |
getMergeFields() | List of [:name] | All MERGEFIELD entries |
getMergeFieldNames() | List of Strings | MERGEFIELD names as a flat list |
getFields() | List of [:fieldType, :fieldResult] | All Word fields (DATE, PAGE, TOC, SEQ, …) |
getCharts() | List of [:type, :title, :widthCm, :heightCm] | Embedded chart metadata |
getChartData() | List with full series data | Charts with categories and series values |
Form Fields#
| Method | Returns | Description |
|---|---|---|
getFormFields() | List | All form fields (any type) |
getCheckboxes() | List of [:label, :checked] | Checkbox controls |
getDropdowns() | List of [:label, :choices, :default] | Dropdown controls |
getTextInputs() | List of [:label, :value] | Text input controls |
Table Query Methods#
| Method | Returns | Description |
|---|---|---|
getTables() | List of table blocks | All tables with full row/cell data |
getTableLayouts() | List of layout summaries | Dimension and style summary per table |
getTableStyle(blockIndex) | List | Style info for a specific table |
getTableRowProperties() | List | Row heights and header-repeat flags |
getNestedTables() | List | Tables nested inside cell content |
getCellsWithBorders() | List | Cells with explicit border overrides |
getCellsWithPadding() | List | Cells with custom padding |
Table block structure:
block[:rows] — list of rows; each row is a list of cell dicts
block[:colWidths] — column widths in twips
block[:rowHeights] — per-row heights in twips (0 = auto)
block[:rowHRules] — per-row height rule strings ("atLeast" / "exact")
block[:rowIsHeader] — per-row boolean (tblHeader repeat flag)
block[:borderStyle] — table-level border style
block[:headerBg] — header background colour hex
block[:evenRowBg] — even-row stripe colourCell dict structure:
cell[:text] — concatenated plain text
cell[:runs] — list of runs: [:text, :bold, :italic, :color, :font,
:size, :lang, :styleName]
cell[:bgColor] — background colour hex
cell[:align] — text alignment
cell[:vAlign] — vertical alignment
cell[:colspan] — column span count
cell[:rowspan] — row span count
cell[:borderStyle] — legacy all-sides border style
cell[:borderSides] — list of per-side entries: [:side, :style, :color, :size]
cell[:textDir] — "btLr", "tbRl", or "" (normal)
cell[:padTop/Bottom/Left/Right] — padding in twipsImage Query Methods#
| Method | Returns | Description |
|---|---|---|
getImages() | List of image blocks | All inline and floating images |
getImagesWithAlt() | List of [:path, :altText, :widthCm, :heightCm] | Images with alt text |
getFloatingImages() | List | Floating images with position and wrap data |
getFloatingImageWrapTypes() | List of [:wrapType, :wrapSide] | Wrap type summary |
getLandscapeSections() | List | Sections using landscape orientation |
Image blocks include crop data when present:
block[:widthCm] — display width in cm
block[:heightCm] — display height in cm
block[:floating] — true for floating images
block[:altText] — accessibility description
block[:cropL] — left crop in percent
block[:cropR] — right crop in percent
block[:cropT] — top crop in percent
block[:cropB] — bottom crop in percentSection and Layout Queries#
| Method | Returns | Description |
|---|---|---|
getSectionLayouts() | List | Per-section column, header/footer info |
getSectionBreaks() | List | Section break points with type and column settings |
getSectionHeaders() | List | Section-level header/footer text |
getSectionLayouts() entry structure:
item[:breakType] — "nextPage", "continuous", "evenPage", "oddPage"
item[:numColumns] — column count in this section
item[:columnSpaceCm] — column gap in cm
item[:sectHeader] — section-specific header text
item[:sectFooter] — section-specific footer textgetSectionBreaks() entry structure:
item[:breakType] — break type string
item[:numColumns] — column count after the break
item[:columnSpaceCm] — column spacing in cmRound-Trip: toWriter()#
toWriter() rebuilds the entire parsed document as a WordWriter instance. You can then add new content, modify page settings, or just call save() to produce a clean copy.
reader = new WordReader("template.docx")
reader.loadDocx()
writer = reader.toWriter()
# Append new content
writer.addPageBreak()
writer.addHeading("Appendix", 1)
writer.addParagraph("Content appended after round-trip reconstruction.", [])
writer.save("extended_output.docx")
reader.cleanup()Round-trip fidelity table:
| Element | Preserved |
|---|---|
| Paragraphs | Text, bold/italic/underline/strike, colour, font, size, alignment, indent, spacing |
| Run properties | Language (:lang), character style (:charStyle), superscript/subscript |
| Headings 1–6 | Level and text |
| Lists | Bullet and numbered, nesting levels, restart points |
| Tables | All cells, merges, bg colours, per-side borders, row heights, text direction |
| Images | Inline and floating, dimensions, wrap type, alt text, crop percentages |
| Footnotes | Full run-level formatting (bold, italic, colour, size per run) |
| Endnotes | Full run-level formatting |
| Hyperlinks | URL and display text |
| Bookmarks | Name and position |
| Section breaks | Break type, column count, column spacing |
| Page properties | Size, margins, orientation, background, page border |
| Headers/Footers | Default, first-page, even-page |
| Paragraph control | keepNext, keepLines, widowControl=false, noHyphenate=true |
| Comments | Text and author |
| Fields | DATE, PAGE, TOC, SEQ, and other auto-updating fields |
| Watermarks | Text and image |
| RTL | RTL paragraphs and document RTL mode |
Reader Quick Reference#
load "docxlib.ring"
reader = new WordReader("doc.docx") # load a file generated by DOCXLib
reader.loadDocx()
# Metadata
? reader.getTitle() ? reader.getAuthor() ? reader.getPageWidthCm()
# Overview
? reader.summary()
reader.listBlocks()
# Content
headings = reader.getHeadings()
tables = reader.getTables()
images = reader.getImages()
footnotes = reader.getFootnotes()
charts = reader.getCharts()
hyperlinks = reader.getHyperlinks()
listItems = reader.getListItems()
# Run intelligence
langs = reader.getRunLanguages()
styles = reader.getCharStyleRuns()
hiddenTx = reader.getHiddenRuns()
# Table detail
layouts = reader.getTableLayouts()
borders = reader.getCellsWithBorders()
rowProps = reader.getTableRowProperties()
# Layout
sections = reader.getSectionLayouts()
breaks = reader.getSectionBreaks()
# Form fields
boxes = reader.getCheckboxes()
drops = reader.getDropdowns()
inputs = reader.getTextInputs()
# Round-trip
reader.save("output.docx")
reader.cleanup()