vibe.templates_mgmt.updatablereferences¶
docx_reference_updater.py - Update references in Word documents.
This module provides functionality to update Table of Contents (TOC) and cross-references in .docx documents after template processing (e.g., with docxtpl) has removed sections.
Key features: - Recalculates heading numbers based on document's numbering system - Updates TOC entries with correct section numbers - Updates REF field cross-references - Handles SEQ fields (figure/table numbers) - Configurable handling of missing references
Usage
from docx_reference_updater import UpdatableReferences import docx
d = UpdatableReferences(docx.Document("my_doc.docx")) result = d.update_references(missing_reference_action='warning') d.save("updated_doc.docx")
Note: Does NOT update page numbers (would require a Word-compatible renderer).
MissingReferenceAction ¶
How to handle references to removed sections.
NumberFormat ¶
Word numbering formats.
NumberingLevel ¶
Represents a single level in a numbering definition.
NumberingDefinition ¶
Represents a complete numbering definition (abstractNum).
get_level ¶
get_level(level: int) -> NumberingLevel | None
Return the NumberingLevel for the given outline level, or None if not defined.
HeadingInfo ¶
Information about a heading in the document.
ReferenceInfo ¶
Information about a reference (REF field, TOC entry, etc.).
SequenceInfo ¶
Information about a SEQ field (figure/table numbering).
UpdateResult ¶
NumberingParser ¶
Parses and manages Word numbering definitions.
get_definition_for_num_id ¶
get_definition_for_num_id(num_id: str) -> NumberingDefinition | None
Get the numbering definition for a numId.
FieldParser ¶
Parses and manages Word field codes.
parse_field_instruction ¶
parse_field_instruction(instr: str) -> tuple[str, str | None, dict[str, str]]
Parse a field instruction.
Returns: (field_type, target, switches).
DocumentScanner ¶
Scans document for headings, bookmarks, and references.
ReferenceUpdater ¶
Updates references in a Word document.
update_all_references ¶
update_all_references() -> UpdateResult
Update all references in the document.
TOCUpdater ¶
Updates Table of Contents entries.
UpdatableReferences ¶
Wrapper for python-docx Document that adds reference updating capabilities.
This class wraps a docx.Document object and provides methods to update cross-references, TOC entries, and other field references after template processing has modified the document structure.
Usage
doc = UpdatableReferences(docx.Document("template_output.docx")) result = doc.update_references(missing_reference_action='warning') doc.save("final_output.docx")
| Attributes: |
|
|---|
__init__ ¶
__init__(document: Document) -> None
Initialize the wrapper.
| Parameters: |
|
|---|
update_references ¶
update_references(missing_reference_action: str | MissingReferenceAction = 'warning', warning_text: str = 'Error! Reference source not found.', update_toc: bool = True, update_ref_fields: bool = True) -> UpdateResult
Update all references in the document.
This method scans the document for cross-references, TOC entries, and other field references, then updates them to reflect the current document structure (including any changes from template processing).
| Parameters: |
|
|---|
| Returns: |
|
|---|
| Raises: |
|
|---|
get_heading_map ¶
get_heading_map() -> dict[str, HeadingInfo]
Get a mapping of bookmark names to heading information.
Useful for debugging or understanding document structure.
| Returns: |
|
|---|
get_numbering_definitions ¶
get_numbering_definitions() -> dict[str, NumberingDefinition]
Get all numbering definitions in the document.
| Returns: |
|
|---|
get_broken_references ¶
get_broken_references() -> list[ReferenceInfo]
Get list of references that point to non-existent targets.
This can be called before update_references() to preview what references would be affected.
| Returns: |
|
|---|
rescan ¶
rescan() -> None
Force a rescan of the document.
Call this if you've made modifications to the document and want to update the internal state before calling update_references().
save ¶
save(path_or_stream: str | PathLike[str] | IO[bytes] | None = None) -> None
Save the document.
| Parameters: |
|
|---|
update_document_references ¶
update_document_references(input_path: str, output_path: str | None = None, missing_reference_action: str = 'warning') -> UpdateResult
Update references in a document file.
Convenience function for simple use cases.
| Parameters: |
|
|---|
| Returns: |
|
|---|