Skip to content

Programmatic API Classes

These classes are returned by docwow.open() and are the primary interface for reading, editing, and building documents in Python. They live in docwow.api but you do not need to import them directly — docwow.open() returns a DocumentWrapper automatically.

DocumentWrapper

docwow.api.DocumentWrapper

Mutable document object returned by docwow.open().

All document content is accessible and editable through this object. Call :meth:save or :meth:to_bytes to produce a DOCX file. Call :meth:to_html to produce an HTML string.

comments property

All comment bodies in the document.

endnotes property

All endnote bodies in the document.

footer property writable

The default-page footer (created on first access if absent).

footer_even property writable

The even-page footer (None if not set).

footer_first property writable

The first-page footer (None if not set).

footnotes property

All footnote bodies in the document.

header property writable

The default-page header (created on first access if absent).

header_even property writable

The even-page header (None if not set).

header_first property writable

The first-page header (None if not set).

margin_bottom_pt property

Bottom margin in points.

margin_left_pt property

Left margin in points.

margin_right_pt property

Right margin in points.

margin_top_pt property

Top margin in points.

page_height_pt property

Page height in points.

page_width_pt property

Page width in points.

paragraphs property

The collection of body elements (paragraphs, lists, images, tables).

title_pg property writable

Whether the first page uses a different header/footer.

add_comment(author='', text='', date='', initials='')

Create a new comment body, register it, and return it.

The returned :class:~docwow.api.comment.MutableComment has an auto-assigned sequential comment_id. The optional text argument adds an initial paragraph with that content. Add a matching :meth:~docwow.api.run.RunCollection.add_comment_ref to the body paragraph where the superscript marker should appear.

Parameters:

Name Type Description Default
author str

Display name of the comment author.

''
text str

Initial comment text (creates one paragraph if non-empty).

''
date str

ISO-8601 datetime string, e.g. "2024-01-15T10:30:00Z".

''
initials str

Author initials.

''

add_footnote(note_type='footnote')

Create a new footnote (or endnote) body, register it, and return it.

The returned :class:~docwow.api.footnote.MutableFootnote has an auto-assigned sequential note_id. Add content via its paragraphs collection, then add a matching :meth:~docwow.api.run.RunCollection.add_footnote_ref to the body paragraph where the marker should appear.

Parameters:

Name Type Description Default
note_type str

'footnote' (default) or 'endnote'.

'footnote'

add_numbering_definition(num_fmt='bullet')

Register a new numbering definition and return its num_id.

Use the returned num_id when calling doc.paragraphs.add_list_item(num_id=...).

Parameters:

Name Type Description Default
num_fmt str

List format for all levels — 'bullet', 'decimal', 'lowerLetter', 'upperLetter', 'lowerRoman', 'upperRoman'.

'bullet'

Returns:

Type Description
str

The new num_id string.

find(text)

Return all paragraphs whose text contains text.

Searches the full concatenated text of every paragraph in the document body (case-sensitive). Returns a list of :class:~docwow.api.paragraph.MutableParagraph objects in document order. Returns an empty list when nothing matches.

Example::

matches = doc.find("quarterly revenue")
for para in matches:
    para.set_bold(True)

remove_comment(comment_id)

Remove the comment with the given comment_id.

Also removes any :class:~docwow.api.run.MutableCommentRef markers in the document body that reference this comment.

Raises:

Type Description
KeyError

if no comment with that ID exists.

remove_endnote(note_id)

Remove the endnote with the given note_id.

Also removes any :class:~docwow.api.run.MutableFootnoteRef markers in the document body that reference this endnote.

Raises:

Type Description
KeyError

if no endnote with that ID exists.

remove_footnote(note_id)

Remove the footnote with the given note_id.

Also removes any :class:~docwow.api.run.MutableFootnoteRef markers in the document body that reference this footnote.

Raises:

Type Description
KeyError

if no footnote with that ID exists.

save(path)

Write the document to a DOCX file at the given path.

set_margins(top_pt=72.0, bottom_pt=72.0, left_pt=72.0, right_pt=72.0)

Set all page margins in points.

set_page_size(width_pt, height_pt)

Set the page dimensions in points.

to_bytes()

Serialise the document to DOCX bytes.

to_html(page_view=False)

Render the document to an HTML string.

Parameters:

Name Type Description Default
page_view bool

When True, styles the output as a physical page and adds @media print / @page rules for correct browser printing and PDF export.

False

ParagraphCollection

docwow.api.ParagraphCollection

Ordered mutable collection of body elements (paragraphs, list items, images, tables).

add_image(data, content_type, width_pt, height_pt, alt_text='')

Create and append a new image paragraph, returning it.

add_list_item(text='', level=0, num_id='1')

Create and append a new list item, returning it.

add_page_break()

Append an explicit page break and return it.

add_paragraph(text='', style_id=None)

Create and append a new paragraph, returning it.

add_section_break(break_type='nextPage', page_width_pt=595.28, page_height_pt=841.89, margin_top_pt=72.0, margin_bottom_pt=72.0, margin_left_pt=72.0, margin_right_pt=72.0)

Append a section break and return it.

Parameters:

Name Type Description Default
break_type str

'nextPage' (default), 'evenPage', 'oddPage', or 'continuous'.

'nextPage'
page_width_pt float

Page width in points for the NEW section.

595.28
page_height_pt float

Page height in points for the NEW section.

841.89
margin_top_pt float

Top margin in points.

72.0
margin_bottom_pt float

Bottom margin in points.

72.0
margin_left_pt float

Left margin in points.

72.0
margin_right_pt float

Right margin in points.

72.0

add_table(rows, cols, width_pt=None, style_id=None)

Create and append a new table with rows × cols empty cells, returning it.

Parameters:

Name Type Description Default
rows int

Number of rows.

required
cols int

Number of columns (cells per row).

required
width_pt float | None

Optional total table width in points.

None
style_id str | None

Optional Word table style ID (e.g. 'TableGrid').

None

add_toc(title='Contents')

Create and append a new Table of Contents, returning it.

Parameters:

Name Type Description Default
title str

Heading text shown above the TOC list. Defaults to "Contents".

'Contents'

append(item)

Append a paragraph or table to the collection.

clear()

Remove all items.

find(text)

Return all paragraphs whose text contains text (case-sensitive).

Returns a list of :class:MutableParagraph objects in document order. Tables, images, and section breaks are not searched.

Example::

for para in doc.paragraphs.find("action item"):
    para.set_bold(True)

insert(index, item)

Insert a paragraph or table at the given index.

remove(index)

Remove the item at the given index.

MutableParagraph

docwow.api.MutableParagraph

A mutable paragraph containing an ordered sequence of runs.

alignment property

Text alignment: 'left', 'center', 'right', 'justify', or None.

borders property

Paragraph borders as a :class:~docwow.models.styles.ParagraphBorders, or None.

indent_first_line_pt property

First-line indent in points (negative = hanging indent).

indent_left_pt property

Left indent in points.

indent_right_pt property

Right indent in points.

keep_together property

Whether all lines of this paragraph are kept on the same page.

keep_with_next property

Whether this paragraph is kept on the same page as the following one.

line_spacing_pt property

Exact line spacing in points, or None for automatic.

list_info property

List metadata if this paragraph is a list item, else None.

page_break_before property

Whether a page break is forced before this paragraph.

runs property

The collection of runs in this paragraph.

shading property

Background shading color as a 6-digit hex RGB string (e.g. '4472C4'), or None.

space_after_pt property

Space after the paragraph in points.

space_before_pt property

Space before the paragraph in points.

style_id property

Named Word style ID (e.g. 'Heading1', 'Normal').

tab_stops property

Tuple of TabStop objects defining custom tab stop positions.

find(text)

Return all text runs in this paragraph whose text contains text (case-sensitive).

Example::

for run in para.find("total"):
    run.set_bold(True)

get_text()

Return the concatenated text of all runs.

set_alignment(alignment)

Set alignment: 'left', 'center', 'right', 'justify', or None.

set_bold(value=True)

Set bold on all text runs.

set_borders(borders)

Set paragraph borders. Pass a :class:~docwow.models.styles.ParagraphBorders or None to clear.

set_color(hex_rgb)

Set text color (hex RGB) on all text runs.

set_font_name(name)

Set font name on all text runs.

set_font_size(pt)

Set font size (in points) on all text runs.

set_indent(left_pt=0.0, right_pt=0.0, first_line_pt=0.0)

Set paragraph indentation in points.

set_italic(value=True)

Set italic on all text runs.

set_keep_together(value=True)

Keep paragraph lines together across page breaks.

set_keep_with_next(value=True)

Keep this paragraph on the same page as the following paragraph.

set_page_break_before(value=True)

Force a page break before this paragraph.

set_shading(hex_rgb)

Set the paragraph background shading color (6-digit hex RGB, e.g. '4472C4') or None to clear.

set_spacing(before_pt=0.0, after_pt=0.0, line_pt=None)

Set paragraph spacing in points.

set_style(style_id)

Set the paragraph style by style ID.

set_tab_stops(stops)

Set custom tab stops. Pass a tuple of :class:~docwow.models.styles.TabStop objects, or () to clear.

set_text(text)

Replace all runs with a single run containing the given text.

set_underline(value=True)

Set underline on all text runs.

MutableListItem

docwow.api.MutableListItem

Bases: MutableParagraph

A paragraph that is a list item.

Inherits all MutableParagraph methods and adds list-specific control over level and numbering definition ID.

level property

List nesting level, 0-based (0 = top level, 8 = maximum depth).

num_id property

Numbering definition ID — links this item to a list created with add_numbering_definition().

set_level(level)

Set the list nesting level (0–8).

set_num_id(num_id)

Set the numbering definition ID.

RunCollection

docwow.api.RunCollection

Ordered mutable collection of run instances.

add_bookmark(name)

Create a bookmark anchor, append it, and return it.

Parameters:

Name Type Description Default
name str

The bookmark name used as the id attribute in HTML and the w:name attribute in OOXML. Must be unique within the document.

required

add_comment_ref(comment_id)

Create a comment reference marker, append it, and return it.

Parameters:

Name Type Description Default
comment_id int

The integer ID of the comment this marker points to.

required

add_cross_ref(bookmark_name, display_text='')

Create a cross-reference to a named bookmark, append it, and return it.

Parameters:

Name Type Description Default
bookmark_name str

The target bookmark name (must match a MutableBookmark elsewhere in the document).

required
display_text str

The text displayed at the reference location. Falls back to bookmark_name if empty.

''

add_deletion(text, author='', date='', change_id=0)

Create a tracked deletion, append it, and return it.

In HTML this renders as a red-strikethrough <del> element. In DOCX it becomes a <w:del> element visible in Word's review pane.

Parameters:

Name Type Description Default
text str

The deleted text.

required
author str

Reviewer name shown in Word's review pane.

''
date str

ISO-8601 timestamp string.

''
change_id int

Optional integer ID; auto-assigned on write if 0.

0

add_floating_image(data, content_type, width_pt, height_pt, pos_h_pt=0.0, pos_v_pt=0.0, h_anchor='column', v_anchor='paragraph', wrap='square', behind_doc=False, alt_text='')

Create a floating image anchored to this paragraph, append it, and return it.

The image floats outside the normal text flow and is positioned by pos_h_pt / pos_v_pt offsets relative to h_anchor / v_anchor.

Parameters:

Name Type Description Default
data bytes

Raw image bytes (PNG, JPEG, etc.).

required
content_type str

MIME type, e.g. 'image/png'.

required
width_pt float

Rendered width in points.

required
height_pt float

Rendered height in points.

required
pos_h_pt float

Horizontal offset from the anchor in points.

0.0
pos_v_pt float

Vertical offset from the anchor in points.

0.0
h_anchor str

Horizontal anchor — 'page', 'margin', or 'column'.

'column'
v_anchor str

Vertical anchor — 'page', 'margin', or 'paragraph'.

'paragraph'
wrap str

Text wrap mode — 'square', 'tight', 'none', 'through', or 'topAndBottom'.

'square'
behind_doc bool

If True, the image appears behind body text.

False
alt_text str

Accessibility description.

''

add_footnote_ref(note_id, note_type='footnote')

Create a footnote or endnote reference marker, append it, and return it.

Parameters:

Name Type Description Default
note_id int

The integer ID of the note this marker points to.

required
note_type str

'footnote' (default) or 'endnote'.

'footnote'

Create a MutableHyperlink, append it, and return it.

add_insertion(text, author='', date='', change_id=0)

Create a tracked insertion, append it, and return it.

In HTML this renders as a green-underlined <ins> element. In DOCX it becomes a <w:ins> element visible in Word's review pane.

Parameters:

Name Type Description Default
text str

The inserted text.

required
author str

Reviewer name shown in Word's review pane.

''
date str

ISO-8601 timestamp string (e.g. "2025-07-10T09:00:00Z").

''
change_id int

Optional integer ID; auto-assigned on write if 0.

0

add_page_number(field_type='PAGE')

Create a MutablePageNumberField, append it, and return it.

Parameters:

Name Type Description Default
field_type str

One of 'PAGE', 'NUMPAGES', 'SECTIONPAGES', 'DATE', 'TIME', 'AUTHOR', 'TITLE', 'FILENAME'.

'PAGE'

add_text(text, bold=False, italic=False, underline=False, strike=False, small_caps=False, all_caps=False, vanish=False, font_name=None, font_size=None, color=None, highlight=None, vertical_align=None, char_style_id=None)

Create a MutableRun, append it, and return it.

append(run)

Append a run to the end of the collection.

clear()

Remove all runs.

insert(index, run)

Insert a run at the given index.

remove(index)

Remove the run at the given index.

MutableRun

docwow.api.MutableRun

A mutable text run. Wraps the state of a frozen TextRun.

all_caps property

True if the run uses all-caps formatting.

bold property

True if the run is bold.

char_style_id property

Named Word character style ID (e.g. 'Strong', 'Emphasis'), or None.

color property

Text colour as a 6-digit hex RGB string (e.g. 'FF0000'), or None.

font_name property

Font family name, or None to inherit from the style.

font_size property

Font size in points, or None to inherit from the style.

highlight property

Highlight colour name (e.g. 'yellow', 'cyan'), or None.

italic property

True if the run is italic.

small_caps property

True if the run uses small-caps formatting.

strike property

True if the run has strikethrough.

underline property

True if the run is underlined.

vanish property

True if the run is hidden (w:vanish).

vertical_align property

Vertical alignment: 'superscript', 'subscript', or None.

get_text()

Return the run's text content.

set_all_caps(value=True)

Set all-caps formatting (renders all letters as uppercase).

set_bold(value=True)

Set bold formatting.

set_char_style(style_id)

Apply a named Word character style (e.g. 'Strong', 'Emphasis') or None to clear.

set_color(hex_rgb)

Set the text color as a hex RGB string (e.g. 'FF0000').

set_font_name(name)

Set the font family name.

set_font_size(pt)

Set the font size in points.

set_highlight(color_name)

Set the highlight color name (e.g. 'yellow').

set_italic(value=True)

Set italic formatting.

set_small_caps(value=True)

Set small-caps formatting (renders lowercase letters as smaller uppercase).

set_strike(value=True)

Set strikethrough formatting.

set_text(text)

Replace the run's text content.

set_underline(value=True)

Set underline formatting.

set_vanish(value=True)

Hide or show this run (Word's hidden text / w:vanish).

set_vertical_align(value)

Set vertical alignment: 'superscript', 'subscript', or None.

MutableImageRun

docwow.api.MutableImageRun

A mutable inline image run.

alt_text property

Image alt text / description.

content_type property

MIME type of the image (e.g. 'image/png', 'image/jpeg').

height_pt property

Rendered height in points.

width_pt property

Rendered width in points.

get_image()

Return the underlying InlineImage.

replace_image(data, content_type, width_pt=None, height_pt=None, alt_text='')

Replace the image bytes and optionally update dimensions.

set_alt_text(alt_text)

Set the image alt text description. Other image properties are unchanged.

set_height_pt(height_pt)

Set the rendered height in points. Other image properties are unchanged.

set_width_pt(width_pt)

Set the rendered width in points. Other image properties are unchanged.

A mutable hyperlink run with a single text string and a URL.

url property

The hyperlink URL.

get_text()

Return the link text.

set_text(text)

Replace the link text.

set_url(url)

Replace the hyperlink URL.

MutablePageNumberField

docwow.api.MutablePageNumberField

A mutable page-number field (PAGE, NUMPAGES, SECTIONPAGES).

field_type property

The field type: 'PAGE', 'NUMPAGES', or 'SECTIONPAGES'.

set_field_type(field_type)

Change the field type.

MutableHeaderFooter

docwow.api.MutableHeaderFooter

A mutable header or footer — an editable collection of paragraphs.

Obtain instances via :attr:DocumentWrapper.header and :attr:DocumentWrapper.footer (or the first/even variants).

Example::

doc = docwow.open("report.docx")

# Edit the default header
hdr = doc.header
hdr.paragraphs.clear()
hdr.paragraphs.add_paragraph("My Company — Confidential")

# Add a footer with page numbers
ftr = doc.footer
ftr.paragraphs.clear()
p = ftr.paragraphs.add_paragraph()
p.runs.add_text("Page ")
p.runs.add_page_number()
p.runs.add_text(" of ")
p.runs.add_page_number("NUMPAGES")

doc.save("output.docx")

paragraphs property

The paragraph collection for this header or footer.

MutableImage

docwow.api.MutableImage

Bases: MutableParagraph

A top-level image in the document body.

Internally represented as a single-run paragraph containing an ImageRun, which is how DOCX stores block-level images. For inline images within text, add a MutableImageRun directly to a paragraph's RunCollection.

alt_text property

Image alt text / description.

content_type property

MIME type of the image (e.g. 'image/png', 'image/jpeg').

height_pt property

Rendered height in points.

width_pt property

Rendered width in points.

replace(data, content_type, width_pt=None, height_pt=None, alt_text='')

Replace the image content, optionally updating dimensions.

MutableTable

docwow.api.MutableTable

A mutable table with rows, cells, and paragraph content.

col_widths_pt property

Column widths in points (may be empty if not set).

style_id property

Word table style ID (e.g. 'TableGrid', 'TableNormal').

width_pt property

Total table width in points, or None for automatic.

add_row(num_cells=0, height_pt=None, cell_width_pt=None)

Create and append a new row with num_cells empty cells, returning it.

Parameters:

Name Type Description Default
num_cells int

Number of empty cells to create in the row.

0
height_pt float | None

Optional row height in points.

None
cell_width_pt float | None

Optional width to assign to each new cell.

None

append(row)

Append a row to the table.

insert(index, row)

Insert a row at the given index.

remove(index)

Remove the row at the given index.

set_col_widths_pt(widths)

Set the column widths in points.

set_style(style_id)

Set the table style by style ID.

set_width_pt(width_pt)

Set the total table width in points.

MutableTableRow

docwow.api.MutableTableRow

A mutable table row containing an ordered sequence of cells.

height_pt property

Row height in points, or None for automatic.

add_cell(width_pt=None)

Create and append a new empty cell, returning it.

append(cell)

Append a cell to the end of the row.

insert(index, cell)

Insert a cell at the given index.

remove(index)

Remove the cell at the given index.

set_height_pt(height_pt)

Set the row height in points.

MutableTableCell

docwow.api.MutableTableCell

A mutable table cell containing an ordered sequence of paragraphs.

col_span property

Number of grid columns this cell spans (default 1).

paragraphs property

The mutable paragraph collection for this cell.

row_span property

Number of rows this cell spans (default 1).

shading property

Cell background shading color as a 6-digit hex RGB string (e.g. 'ED7D31'), or None.

v_merge_continue property

True if this cell continues a vertical merge (visually spanned by the cell above).

v_merge_start property

True if this cell begins a vertical merge group (OOXML w:vMerge w:val="restart").

width_pt property

Cell width in points, or None for automatic.

get_text()

Return the concatenated text of all runs in all paragraphs.

set_col_span(value)

Set the column span.

set_row_span(value)

Set the row span.

set_shading(hex_rgb)

Set the cell background shading color (6-digit hex RGB, e.g. 'ED7D31') or None to clear.

set_width_pt(width_pt)

Set the cell width in points.

MutableBookmark

docwow.api.MutableBookmark

A mutable bookmark anchor.

When converted to a frozen model (at save time) this produces a :class:~docwow.models.paragraph.BookmarkStart which the writer renders as a <w:bookmarkStart> / <w:bookmarkEnd> pair.

name property

The bookmark name used as the HTML id and OOXML w:name.

set_name(name)

Replace the bookmark name.

MutableTableOfContents

docwow.api.MutableTableOfContents

A mutable Table of Contents.

Wraps a :class:~docwow.models.toc.TableOfContents and provides a chainable API for editing TOC entries.

Example::

toc = MutableTableOfContents(title="Contents")
toc.add_entry("Introduction", url="#_Toc1", level=1)
toc.add_entry("Background", url="#_Toc2", level=2)

entries property

Ordered list of :class:MutableTocEntry objects.

title property

Heading text shown above the entry list.

add_entry(text, url='', level=1)

Append a new entry and return it.

Parameters:

Name Type Description Default
text str

Visible label of the entry.

required
url str

Anchor target (e.g. "#_Toc123"). Defaults to "".

''
level int

Depth (1–9). Defaults to 1.

1

clear_entries()

Remove all entries. Returns self for chaining.

remove_entry(entry)

Remove entry from the TOC.

Raises:

Type Description
ValueError

if the entry is not in this TOC.

set_title(title)

Set the TOC heading text. Returns self for chaining.

MutableTocEntry

docwow.api.MutableTocEntry

A mutable entry in a Table of Contents.

level property

Hierarchy depth, 1–9.

text property

Visible label of this entry.

url property

Anchor target URL (e.g. "#_Toc123") or empty string.

set_level(level)

Set the depth level (1–9). Returns self for chaining.

set_text(text)

Set the display text. Returns self for chaining.

set_url(url)

Set the anchor URL. Returns self for chaining.

MutableFootnote

docwow.api.MutableFootnote

A mutable footnote or endnote body.

Access the note body content via :attr:paragraphs.

note_id property

The integer ID of this note (matches the in-body reference marker).

note_type property

'footnote' or 'endnote'.

paragraphs property

The mutable paragraph collection for this note's content.

get_text()

Return the concatenated text of all runs in all paragraphs.

MutableFootnoteRef

docwow.api.MutableFootnoteRef

A mutable inline footnote or endnote reference marker.

This is placed inside a paragraph's :class:~docwow.api.run.RunCollection to mark where the superscript reference appears in the text.

note_id property

The integer ID of this reference (matches the note body's note_id).

note_type property

'footnote' or 'endnote'.

get_text()

Footnote refs have no direct text content.

MutableComment

docwow.api.MutableComment

A mutable document comment (annotation).

Access the comment text via :attr:paragraphs. Use :meth:set_author and :meth:set_date to change metadata.

author property

Display name of the comment author.

comment_id property

The integer ID of this comment (matches the in-body reference marker).

date property

ISO-8601 datetime string when the comment was made, or empty string.

initials property

Author initials as stored in the DOCX.

paragraphs property

Mutable paragraph collection for this comment's text content.

get_text()

Return the concatenated text of all runs in all paragraphs.

set_author(author)

Set the comment author display name.

set_date(date)

Set the comment date (ISO-8601 string, e.g. "2024-01-15T10:30:00Z").

set_initials(initials)

Set the author initials.

MutableCommentRef

docwow.api.MutableCommentRef

A mutable inline comment reference marker.

Place this inside a paragraph's :class:~docwow.api.run.RunCollection to mark where the comment superscript appears in the text.

comment_id property

The integer ID of this reference (matches the comment body's comment_id).

get_text()

Comment refs have no direct text content.

MutableTrackedChange

docwow.api.MutableTrackedChange

A mutable tracked change (insertion or deletion).

Wraps a w:ins or w:del element. change_type is either "insert" or "delete".

Create via :meth:RunCollection.add_insertion or :meth:RunCollection.add_deletion rather than instantiating directly.

author property

Reviewer display name.

change_id property

OOXML w:id for this change (0 = auto-assigned on write).

change_type property

Either 'insert' or 'delete'.

date property

ISO-8601 datetime string of the change.

get_text()

Return the changed text.

set_author(author)

Set the author name.

set_date(date)

Set the ISO-8601 date string.

set_text(text)

Replace the changed text.