Insert Nested Fields in Python

Aspose.Words - Insert Nested Fields

Python Code

doc = self.Document()
builder = self.DocumentBuilder(doc)
\# Insert few page breaks (just for testing)
i = 0
while(i < 5):
    builder.insertBreak(self.BreakType.PAGE_BREAK)
    i = i + 1
\# Move DocumentBuilder cursor into the primary footer.
builder.moveToHeaderFooter(self.HeaderFooterType.FOOTER_PRIMARY)
\# We want to insert a field like this:
\# { IF {PAGE} <> {NUMPAGES} "See Next Page" "Last Page" }
field = builder.insertField("IF ")
builder.moveTo(field.getSeparator())
builder.insertField("PAGE")
builder.write(" <> ")
builder.insertField("NUMPAGES")
builder.write(" \"See Next Page\" \"Last Page\" ")
\# Finally update the outer field to recalculate the final value. Doing this will automatically update
\# the inner fields at the same time.
field.update()
doc.save(self.dataDir + "InsertNestedFields Out.docx")
    }

Download Running Code

Download Insert Nested Fields (Aspose.Words) from any of the below mentioned social coding sites: