docx_plus.controls.read¶
Read and modify the values of existing content controls.
read_controls(doc) returns a flat dict[str, ControlValue] keyed by
tag (default) or alias; set_control_value / clear_control mutate
single controls.
The five typed errors are all dual-base (DocxPlusError plus a stdlib
exception) so callers can match either contract — see
ARCHITECTURE.md §9.
docx_plus.controls.read ¶
Read and modify content controls (SDTs) in an existing document.
The companion to :mod:docx_plus.controls.builder. Where builder writes
w:sdt elements, this module discovers them, reports their values, sets new
values, or resets them to placeholder state.
The read side is intentionally schema-tolerant: it works on any document with
content controls, not just ones built by :class:FormBuilder. Type detection
dispatches on the marker child of w:sdtPr (w:text, w:dropDownList,
w:comboBox, w:date, w14:checkbox).
ControlType
module-attribute
¶
ControlValue
dataclass
¶
ControlValue(
tag: str,
alias: str | None,
control_type: ControlType,
value: ControlValueT | None,
is_placeholder: bool,
)
A single content control's identity, type, and current value.
Attributes:
| Name | Type | Description |
|---|---|---|
tag |
str
|
The control's |
alias |
str | None
|
The control's |
control_type |
ControlType
|
One of |
value |
ControlValueT | None
|
The current value:
|
is_placeholder |
bool
|
True if the control is showing its placeholder text
( |
ControlNotFoundError ¶
Bases: DocxPlusError, KeyError
Raised when no content control with the requested tag exists.
Subclasses KeyError so existing except KeyError: clauses still
catch it; also subclasses :class:DocxPlusError per SPEC §9.7.
DuplicateTagError ¶
Bases: DocxPlusError, ValueError
Raised by :func:read_controls when two controls share a key.
ValueNotInListError ¶
Bases: DocxPlusError, ValueError
Raised by :func:set_control_value when a dropdown value has no match.
ControlTypeError ¶
Bases: DocxPlusError, TypeError
Raised when a value's Python type does not match the control's type.
read_controls ¶
Return every content control in doc keyed by tag (or alias).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
The python-docx Document to inspect. |
required |
by
|
Literal['tag', 'alias']
|
Either |
'tag'
|
Returns:
| Type | Description |
|---|---|
dict[str, ControlValue]
|
Mapping from key to :class: |
Raises:
| Type | Description |
|---|---|
DuplicateTagError
|
If two controls share the same key. |
Source code in docx_plus/controls/read.py
set_control_value ¶
Set the value of a control identified by tag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
The python-docx Document to modify. |
required |
tag
|
str
|
The control's |
required |
value
|
ControlValueT
|
The new value. Type must match the control type:
|
required |
Raises:
| Type | Description |
|---|---|
ControlNotFoundError
|
If no control with that tag exists. |
ControlTypeError
|
If |
ValueNotInListError
|
For a dropdown when |
Source code in docx_plus/controls/read.py
clear_control ¶
Reset a control to its placeholder state.
For text/dropdown/combobox/date: re-adds w:showingPlcHdr to sdtPr and
re-applies the PlaceholderText rStyle to every run in sdtContent. The
placeholder text itself is preserved in place (whatever sdtContent
currently holds).
For checkbox: resets the checked flag to 0 and the glyph to
☐. Checkboxes have no placeholder mode.