docx_plus.controls.builder¶
Build content controls (SDTs) — text, dropdown, date, checkbox.
FormBuilder wraps a python-docx Document and emits valid w:sdt
blocks that round-trip through Word.
Architecture walkthrough: ARCHITECTURE.md §6.
docx_plus.controls.builder ¶
Build Word content controls (SDTs) — text, dropdown, date, checkbox.
python-docx stops at the paragraph/run layer; content controls are w:sdt
elements that have to be synthesised at the lxml level. :class:FormBuilder
wraps a python-docx :class:~docx.document.Document and provides add_*
methods that emit valid w:sdt blocks and append them inline to a
paragraph.
The builder handles the three failure modes the docx-forms skill prototype identified:
w:idcollisions — every id flows through :class:IdRegistry.- The latent
PlaceholderTextstyle — materialised on construction so the grey placeholder text actually renders. w14namespace declaration on the document root — required byw14:checkbox; verified at construction time.
This module imports only from docx_plus.core (SPEC §9.1). The
PlaceholderText style definition is duplicated here intentionally rather
than reused from :mod:docx_plus.styles.modify.
FormBuilder ¶
FormBuilder(
document_or_path: Document | str | PathLike[str] | None = None,
*,
id_registry: IdRegistry | None = None,
)
Wrap a python-docx Document and add fillable content controls.
self.doc is the underlying :class:~docx.document.Document — use it
for ordinary document construction (headings, paragraphs, tables). Use the
add_* methods to drop content controls into paragraphs you have made.
Each add_* method appends the SDT inline at the end of the paragraph
you pass, so put the field's label in the paragraph text first.
Open or wrap a document and prepare the builder state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
document_or_path
|
Document | str | PathLike[str] | None
|
An open :class: |
None
|
id_registry
|
IdRegistry | None
|
An existing :class: |
None
|
Raises:
| Type | Description |
|---|---|
MissingNamespaceError
|
If the document root does not declare the
|
Source code in docx_plus/controls/builder.py
add_text_control ¶
add_text_control(
paragraph: Paragraph,
*,
tag: str,
alias: str | None = None,
placeholder: str = "Click to enter text",
multiline: bool = False,
) -> etree._Element
Append an inline plain-text content control to paragraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
The python-docx paragraph to append into. |
required |
tag
|
str
|
Stable machine-readable identifier for the control. |
required |
alias
|
str | None
|
Optional human-friendly label shown in Word's UI. |
None
|
placeholder
|
str
|
The grey "click here" prompt rendered inside the empty control. |
'Click to enter text'
|
multiline
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
_Element
|
The created |
Source code in docx_plus/controls/builder.py
add_dropdown ¶
add_dropdown(
paragraph: Paragraph,
*,
tag: str,
items: list[DropdownItem],
alias: str | None = None,
placeholder: str = "Choose an item",
editable: bool = False,
) -> etree._Element
Append a dropdown (or combobox) content control to paragraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
The python-docx paragraph to append into. |
required |
tag
|
str
|
Stable machine-readable identifier for the control. |
required |
items
|
list[DropdownItem]
|
A list of either plain strings, or |
required |
alias
|
str | None
|
Optional human-friendly label shown in Word's UI. |
None
|
placeholder
|
str
|
The "Choose an item" prompt rendered inside the empty control. A placeholder list-item with empty value is also added as the first dropdown entry. |
'Choose an item'
|
editable
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
_Element
|
The created |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in docx_plus/controls/builder.py
add_date_picker ¶
add_date_picker(
paragraph: Paragraph,
*,
tag: str,
alias: str | None = None,
placeholder: str = "Click to select a date",
date_format: str = "M/d/yyyy",
lcid: str = "en-US",
) -> etree._Element
Append a date-picker content control to paragraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
The python-docx paragraph to append into. |
required |
tag
|
str
|
Stable machine-readable identifier for the control. |
required |
alias
|
str | None
|
Optional human-friendly label shown in Word's UI. |
None
|
placeholder
|
str
|
The grey "click here" prompt rendered inside the empty control. |
'Click to select a date'
|
date_format
|
str
|
Word's date-format string (e.g. |
'M/d/yyyy'
|
lcid
|
str
|
Locale identifier (BCP-47 form, e.g. |
'en-US'
|
Returns:
| Type | Description |
|---|---|
_Element
|
The created |
Source code in docx_plus/controls/builder.py
add_checkbox ¶
add_checkbox(
paragraph: Paragraph,
*,
tag: str,
alias: str | None = None,
checked: bool = False,
) -> etree._Element
Append a Word 2010+ w14:checkbox content control to paragraph.
The visible glyph and the w14:checked flag are kept in sync, so
the box renders correctly even before Word ever opens the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
The python-docx paragraph to append into. |
required |
tag
|
str
|
Stable machine-readable identifier for the control. |
required |
alias
|
str | None
|
Optional human-friendly label shown in Word's UI. |
None
|
checked
|
bool
|
Initial checked state. |
False
|
Returns:
| Type | Description |
|---|---|
_Element
|
The created |
Source code in docx_plus/controls/builder.py
save ¶
Save the wrapped document to path and return the path as a string.
MissingNamespaceError ¶
Bases: DocxPlusError
Raised when a required namespace is not declared on the document root.
InvalidDropdownItemError ¶
Bases: DocxPlusError, TypeError
Raised when a dropdown items entry is not a str or (str, str).
Subclasses TypeError so existing except TypeError: clauses still
catch it; also subclasses :class:DocxPlusError per SPEC §9.7.