get_field_code method

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.

def get_field_code(self):
    ...

get_field_code(include_child_field_codes)

Returns text between field start and field separator (or field end if there is no separator).

def get_field_code(self, include_child_field_codes: bool):
    ...
ParameterTypeDescription
include_child_field_codesboolTrue if child field codes should be included.

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))

Shows how to get a field’s field code.

# Open a document which contains a MERGEFIELD inside an IF field.
doc = aw.Document(file_name=MY_DIR + 'Nested fields.docx')
field_if = doc.range.fields[0].as_field_if()
# There are two ways of getting a field's field code:
# 1 -  Omit its inner fields:
self.assertEqual(' IF  > 0 " (surplus of ) " "" ', field_if.get_field_code(False))
# 2 -  Include its inner fields:
self.assertEqual(f' IF \x13 MERGEFIELD NetIncome \x14\x15 > 0 " (surplus of \x13 MERGEFIELD  NetIncome \\f $ \x14\x15) " "" ', field_if.get_field_code(True))
# By default, the GetFieldCode method displays inner fields.
self.assertEqual(field_if.get_field_code(), field_if.get_field_code(True))

See Also