Field class
Field class
Represents a Microsoft Word document field. To learn more, visit the Working with Fields documentation article.
Remarks
A field in a Word document is a complex structure consisting of multiple nodes that include field start, field code, field separator, field result and field end. Fields can be nested, contain rich content and span multiple paragraphs or sections in a document. The Field class is a “facade” object that provides properties and methods that allow to work with a field as a single object.
The Field.start, Field.separator and Field.end properties point to the field start, separator and end nodes of the field respectively.
The content between the field start and separator is the field code. The content between the field separator and field end is the field result. The field code typically consists of one or more Run objects that specify instructions. The processing application is expected to execute the field code to calculate the field result.
The process of calculating field results is called the field update. Aspose.Words can update field results of most of the field types in exactly the same way as Microsoft Word does it. Most notably, Aspose.Words can calculate results of even the most complex formula fields. To calculate the field result of a single field use the Field.update() method. To update fields in the whole document use Document.update_fields().
You can get the plain text version of the field code using the Field.get_field_code() method. You can get and set the plain text version of the field result using the Field.result property. Both the field code and field result can contain complex content, such as nested fields, paragraphs, shapes, tables and in this case you might want to work with the field nodes directly if you need more control.
You do not create instances of the Field class directly. To create a new field use the DocumentBuilder.insert_field() method.
Properties
Name | Description |
---|---|
display_result | Gets the text that represents the displayed field result. |
end | Gets the node that represents the field end. |
format | Gets a FieldFormat object that provides typed access to field’s formatting. |
is_dirty | Gets or sets whether the current result of the field is no longer correct (stale) due to other modifications made to the document. |
is_locked | Gets or sets whether the field is locked (should not recalculate its result). |
locale_id | Gets or sets the LCID of the field. |
result | Gets or sets text that is between the field separator and field end. |
separator | Gets the node that represents the field separator. Can be None . |
start | Gets the node that represents the start of the field. |
type | Gets the Microsoft Word field type. |
Methods
Examples
Shows how to insert a field into a document using a field code.
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
field = builder.insert_field('DATE \\@ "dddd, MMMM dd, yyyy"')
self.assertEqual(aw.fields.FieldType.FIELD_DATE, field.type)
self.assertEqual('DATE \\@ "dddd, MMMM dd, yyyy"', field.get_field_code())
# This overload of the "insert_field" method automatically updates inserted fields.
self.assertAlmostEqual(datetime.datetime.strptime(field.result, '%A, %B %d, %Y'), datetime.datetime.now(), delta=timedelta(1))
See Also
- module aspose.words.fields