Modules@robinpath/docx
docx

@robinpath/docx

0.1.1Node.jsPublic

Word document (.docx) creation and reading for RobinPath

Docx

Word document (.docx) creation and reading — headings, paragraphs, tables, images, lists, hyperlinks, bookmarks, TOC, footnotes, comments, sections, headers/footers, styles, checkboxes, and document patching. Zero npm dependencies.

Package: @robinpath/docx | Category: Document | Type: External

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the docx module when you need to:

  • Create a new Word document -- Use docx.createDoc to start a new document
  • Read an existing Word document -- Use docx.readDoc to extract text or HTML
  • Add headings and paragraphs -- Use docx.addHeading and docx.addParagraph for text content
  • Add tables with styling -- Use docx.addTable for data tables with headers, borders, and colors
  • Add images -- Use docx.addImage to embed images with sizing and alignment
  • Add lists -- Use docx.addList for bulleted or numbered lists
  • Add hyperlinks and bookmarks -- Use docx.addHyperlink and docx.addBookmark for navigation
  • Add table of contents -- Use docx.addTableOfContents for auto-generated TOC
  • Add footnotes and comments -- Use docx.addFootnote and docx.addComment for annotations
  • Patch existing documents -- Use docx.patchDoc to replace placeholders in templates
  • Save the document -- Use docx.saveDoc to write the .docx file

Quick Reference

FunctionDescriptionReturns
createDocCreate a new Word documentstring
readDocRead text or HTML from an existing .docxstring
addHeadingAdd a heading with level and formattingboolean
addParagraphAdd a paragraph with rich text formattingboolean
addTableAdd a table with full stylingboolean
addImageAdd an image to the documentboolean
addPageBreakAdd a page breakboolean
addListAdd a bulleted or numbered listboolean
addHyperlinkAdd a clickable hyperlinkboolean
addBookmarkAdd a named bookmarkboolean
addTableOfContentsAdd an auto-generated table of contentsboolean
addFootnoteAdd a footnote reference and contentnumber
addCommentAdd a comment annotationnumber
addSectionAdd a new section with separate formattingnumber
setDocPropertiesSet document metadata propertiesboolean
addDocStyleDefine a reusable named styleboolean
addHeaderAdd a custom headerboolean
addFooterAdd a custom footer with optional page numbersboolean
patchDocReplace placeholders in an existing .docxObject
addCheckboxAdd a checkbox with labelboolean
saveDocSave the document to a .docx fileObject

Functions

createDoc

Create a new Word document

Module: docx | Returns: string -- Document ID

docx.createDoc "report" { "margins": { "top": 1, "bottom": 1 } }
ParameterTypeRequiredDescription
idstringNoDocument ID
optionsobjectNo{margins: {top, bottom, left, right}}

readDoc

Read text or HTML from an existing Word document

Module: docx | Returns: string -- Document content as text or HTML

docx.readDoc "./contract.docx" "text"
ParameterTypeRequiredDescription
filePathstringYesPath to .docx file
formatstringNo'text' (default) or 'html'

addHeading

Add a heading with level and formatting

Module: docx | Returns: boolean -- true

docx.addHeading $doc "Sales Report" 1 { "color": "#2196F3", "alignment": "center" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
textstringYesHeading text
levelnumberNoHeading level 1-6
optionsobjectNo{bold, italic, fontSize, font, color, alignment, spacing}

addParagraph

Add a paragraph with rich text formatting

Module: docx | Returns: boolean -- true

docx.addParagraph $doc "Revenue increased by 23%" { "bold": true, "fontSize": 12, "alignment": "justify" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
textstringYesParagraph text
optionsobjectNo{bold, italic, underline, fontSize, font, color, alignment, spacing, indent, runs}

addTable

Add a table with full styling (headers, borders, colors, widths)

Module: docx | Returns: boolean -- true

docx.addTable $doc $data { "headerStyle": { "fillColor": "#2196F3", "fontColor": "#FFFFFF" }, "alternateRows": true }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
dataarrayYesArray of objects or array of arrays
optionsobjectNo{headerStyle, cellStyle, columnWidths, borders, alternateRows}

addImage

Add an image to the document

Module: docx | Returns: boolean -- true

docx.addImage $doc "./chart.png" { "width": 500, "height": 300, "alignment": "center" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
imagePathstringYesPath to image file
optionsobjectNo{width, height, alignment}

addPageBreak

Add a page break

Module: docx | Returns: boolean -- true

docx.addPageBreak $doc
ParameterTypeRequiredDescription
docIdstringYesDocument ID

addList

Add a bulleted or numbered list

Module: docx | Returns: boolean -- true

docx.addList $doc ["Item 1", "Item 2", "Item 3"] { "ordered": true, "fontSize": 11 }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
itemsarrayYesArray of strings
optionsobjectNo{ordered: bool, bold, fontSize, font, color}

addHyperlink

Add a clickable hyperlink to the document

Module: docx | Returns: boolean -- true

docx.addHyperlink $doc "Visit our site" "https://example.com" { "bold": true }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
textstringYesDisplay text
urlstringYesTarget URL
optionsobjectNo{color, bold, fontSize, font, underline, alignment}

addBookmark

Add a named bookmark for cross-references

Module: docx | Returns: boolean -- true

docx.addBookmark $doc "section1" "Introduction" { "bold": true }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
namestringYesBookmark name
textstringYesBookmark text
optionsobjectNo{bold, fontSize, font, color}

addTableOfContents

Add an auto-generated table of contents from headings

Module: docx | Returns: boolean -- true

docx.addTableOfContents $doc { "heading": "Contents", "headingRange": "1-3" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
optionsobjectNo{heading, hyperlink, headingRange}

addFootnote

Add a footnote reference in text with footnote content at bottom

Module: docx | Returns: number -- Footnote ID

docx.addFootnote $doc "See reference" "Source: IEEE 2024 paper" {}
ParameterTypeRequiredDescription
docIdstringYesDocument ID
textstringYesParagraph text
footnoteTextstringYesFootnote content
optionsobjectNo{bold, fontSize, font, color, alignment}

addComment

Add a comment annotation on a text range

Module: docx | Returns: number -- Comment ID

docx.addComment $doc "This needs review" "Please verify the figures" { "author": "Editor" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
textstringYesCommented text
commentTextstringYesComment content
optionsobjectNo{author, bold, fontSize}

addSection

Add a new document section with separate formatting (orientation, columns, margins)

Module: docx | Returns: number -- Section index

docx.addSection $doc { "orientation": "landscape", "type": "nextPage" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
optionsobjectNo{type: nextPage/continuous/evenPage/oddPage, orientation, columns, margins}

setDocProperties

Set document metadata properties (title, author, keywords)

Module: docx | Returns: boolean -- true

docx.setDocProperties $doc { "title": "Annual Report", "author": "Finance Dept" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
optionsobjectYes{title, author, subject, keywords, description}

addDocStyle

Define a reusable named paragraph or character style

Module: docx | Returns: boolean -- true

docx.addDocStyle $doc "Code Block" { "font": "Courier New", "fontSize": 10, "color": "333333" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
styleNamestringYesStyle name
optionsobjectNo{type, font, fontSize, bold, italic, color, alignment, spacing, basedOn}

addHeader

Add a custom header to the current section

Module: docx | Returns: boolean -- true

docx.addHeader $doc "Company Report 2026" { "alignment": "right", "fontSize": 9 }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
textstringYesHeader text
optionsobjectNo{type: default/first/even, alignment, bold, fontSize, font, color}

addFooter

Add a custom footer with optional page numbers

Module: docx | Returns: boolean -- true

docx.addFooter $doc "Confidential" { "pageNumbers": true, "alignment": "center" }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
textstringYesFooter text
optionsobjectNo{type: default/first/even, alignment, pageNumbers, pageNumberAlignment}

patchDoc

Modify an existing .docx by replacing placeholders with new content

Module: docx | Returns: Object -- {path}

docx.patchDoc "./template.docx" "./output.docx" { "{{NAME}}": "John", "{{DATE}}": "2026-01-15" }
ParameterTypeRequiredDescription
inputPathstringYesPath to existing .docx
outputPathstringYesOutput .docx path
patchesobjectYes{"{{PLACEHOLDER}}": "replacement text"}
optionsobjectNo{keepStyles}

addCheckbox

Add a checkbox with label text

Module: docx | Returns: boolean -- true

docx.addCheckbox $doc "I agree to the terms" { "checked": false }
ParameterTypeRequiredDescription
docIdstringYesDocument ID
labelstringYesCheckbox label
optionsobjectNo{checked: bool, bold, fontSize, font, color}

saveDoc

Save the Word document to a .docx file

Module: docx | Returns: Object -- {path, size}

docx.saveDoc $doc "./report.docx"
ParameterTypeRequiredDescription
docIdstringYesDocument ID
filePathstringYesOutput .docx path

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Document not foundInvalid document ID
File not found.docx file does not exist at path
Invalid formatCorrupted or non-OOXML file
@desc "Create document with error handling"
do
  set $doc as docx.createDoc "report"
  if $doc != null
    docx.addHeading $doc "My Report" 1
    docx.saveDoc $doc "./report.docx"
    print "Document saved"
  else
    print "Failed to create document"
  end
enddo

Recipes

1. Create a full business report

Build a complete Word document with headings, paragraphs, table, and formatting.

@desc "Generate a quarterly sales report"
do
  set $doc as docx.createDoc "q4-report" { "margins": { "top": 1, "bottom": 1, "left": 1.25, "right": 1.25 } }
  docx.setDocProperties $doc { "title": "Q4 Sales Report", "author": "Sales Team" }
  docx.addHeader $doc "Q4 2026 Sales Report" { "alignment": "right", "fontSize": 9 }
  docx.addFooter $doc "Confidential" { "pageNumbers": true, "alignment": "center" }
  docx.addHeading $doc "Quarterly Sales Report" 1 { "color": "#2196F3", "alignment": "center" }
  docx.addParagraph $doc "This report covers sales performance for Q4 2026." { "fontSize": 12, "spacing": { "after": 200 } }
  docx.addHeading $doc "Revenue Summary" 2
  set $data as [
    { "Region": "North", "Revenue": "$1.2M", "Growth": "+15%" },
    { "Region": "South", "Revenue": "$890K", "Growth": "+8%" },
    { "Region": "West", "Revenue": "$1.5M", "Growth": "+22%" }
  ]
  docx.addTable $doc $data { "headerStyle": { "fillColor": "#2196F3", "fontColor": "#FFFFFF" }, "alternateRows": true }
  docx.addPageBreak $doc
  docx.addHeading $doc "Key Takeaways" 2
  docx.addList $doc ["West region leads with 22% growth", "Overall revenue up 15% YoY", "New product line contributed 30% of growth"] { "ordered": false }
  docx.saveDoc $doc "./q4-report.docx"
  print "Report saved to q4-report.docx"
enddo

2. Read and extract text from a document

Read an existing .docx and process its content.

@desc "Extract text from a contract"
do
  set $content as docx.readDoc "./contract.docx" "text"
  set $lines as string.split $content "\n"
  print "Document has " + array.length $lines + " lines"
  each $line in $lines
    if string.contains $line "Term"
      print "Found: " + $line
    end
  endeach
enddo

3. Template-based document generation

Use patchDoc to fill in a template with dynamic data.

@desc "Generate contract from template"
do
  set $patches as {
    "{{CLIENT_NAME}}": "Acme Corp",
    "{{DATE}}": "2026-03-13",
    "{{AMOUNT}}": "$50,000",
    "{{TERMS}}": "Net 30"
  }
  set $result as docx.patchDoc "./contract-template.docx" "./contract-acme.docx" $patches
  print "Contract generated at: " + $result.path
enddo

4. Document with images and styles

Create a styled document with custom formatting and images.

@desc "Create a product catalog page"
do
  set $doc as docx.createDoc "catalog"
  docx.addDocStyle $doc "ProductTitle" { "font": "Arial", "fontSize": 16, "bold": true, "color": "#333333" }
  docx.addDocStyle $doc "Price" { "font": "Arial", "fontSize": 14, "color": "#27ae60", "bold": true }
  docx.addHeading $doc "Product Catalog 2026" 1 { "alignment": "center" }
  docx.addImage $doc "./product-photo.jpg" { "width": 400, "height": 300, "alignment": "center" }
  docx.addParagraph $doc "Premium Widget Pro" { "bold": true, "fontSize": 16 }
  docx.addParagraph $doc "$99.99" { "color": "#27ae60", "bold": true, "fontSize": 14 }
  docx.addParagraph $doc "Our flagship product with advanced features and premium build quality."
  docx.addCheckbox $doc "In Stock" { "checked": true }
  docx.saveDoc $doc "./catalog.docx"
  print "Catalog saved"
enddo

Related Modules

  • pptx -- PowerPoint presentation creation
  • pdf -- PDF document generation
  • excel -- Excel spreadsheet creation
  • file -- File system operations

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/docx

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size18.5 KB
Versions1
Weekly Downloads25
Total Downloads25
Stars0
Last Publish1 months ago
Created1 months ago

Category

productivity