FieldTC class

FieldTC class

Implements the TC field. To learn more, visit the Working with Fields documentation article.

Remarks

Defines the text and page number for a table of contents (including a table of figures) entry, which is used by a TOC field.

Inheritance: FieldTCField

Constructors

NameDescription
FieldTC()The default constructor.

Properties

NameDescription
display_resultGets the text that represents the displayed field result.
(Inherited from Field)
endGets the node that represents the field end.
(Inherited from Field)
entry_levelGets or sets the level of the entry.
formatGets a FieldFormat object that provides typed access to field’s formatting.
(Inherited from Field)
is_dirtyGets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document.
(Inherited from Field)
is_lockedGets or sets whether the field is locked (should not recalculate its result).
(Inherited from Field)
locale_idGets or sets the LCID of the field.
(Inherited from Field)
omit_page_numberGets or sets whether page number in TOC should be omitted for this field.
resultGets or sets text that is between the field separator and field end.
(Inherited from Field)
separatorGets the node that represents the field separator. Can be None.
(Inherited from Field)
startGets the node that represents the start of the field.
(Inherited from Field)
textGets or sets the text of the entry.
typeGets the Microsoft Word field type.
(Inherited from Field)
type_identifierGets or sets a type identifier for this field (which is typically a letter).

Methods

NameDescription
get_field_code()Returns text between field start and field separator (or field end if there is no separator). Both field code and field result of child fields are included.
(Inherited from Field)
get_field_code(include_child_field_codes)Returns text between field start and field separator (or field end if there is no separator).
(Inherited from Field)
remove()Removes the field from the document. Returns a node right after the field. If the field’s end is the last child of its parent node, returns its parent paragraph. If the field is already removed, returns None.
(Inherited from Field)
unlink()Performs the field unlink.
(Inherited from Field)
update()Performs the field update. Throws if the field is being updated already.
(Inherited from Field)
update(ignore_merge_format)Performs a field update. Throws if the field is being updated already.
(Inherited from Field)

Examples

Shows how to insert a TOC field, and filter which TC fields end up as entries.

def field_toc_entry_identifier():
    doc = aw.Document()
    builder = aw.DocumentBuilder(doc)
    # Insert a TOC field, which will compile all TC fields into a table of contents.
    field_toc = builder.insert_field(aw.fields.FieldType.FIELD_TOC, True).as_field_toc()
    # Configure the field only to pick up TC entries of the "A" type, and an entry-level between 1 and 3.
    field_toc.entry_identifier = 'A'
    field_toc.entry_level_range = '1-3'
    self.assertEqual(' TOC  \\f A \\l 1-3', field_toc.get_field_code())
    # These two entries will appear in the table.
    builder.insert_break(aw.BreakType.PAGE_BREAK)
    insert_toc_entry(builder, 'TC field 1', 'A', '1')
    insert_toc_entry(builder, 'TC field 2', 'A', '2')
    self.assertEqual(' TC  "TC field 1" \\n \\f A \\l 1', doc.range.fields[1].get_field_code())
    # This entry will be omitted from the table because it has a different type from "A".
    insert_toc_entry(builder, 'TC field 3', 'B', '1')
    # This entry will be omitted from the table because it has an entry-level outside of the 1-3 range.
    insert_toc_entry(builder, 'TC field 4', 'A', '5')
    doc.update_fields()
    doc.save(ARTIFACTS_DIR + 'Field.tc.docx')

def insert_toc_entry(builder: aw.DocumentBuilder, text: str, type_identifier: str, entry_level: str):
    """Use a document builder to insert a TC field."""
    field_tc = builder.insert_field(aw.fields.FieldType.FIELD_TOC_ENTRY, True).as_field_tc()
    field_tc.omit_page_number = True
    field_tc.text = text
    field_tc.type_identifier = type_identifier
    field_tc.entry_level = entry_level

See Also