How to get all textbox in a section in word

I want to get all textbox in word and check the text.

@Mark030 What object do you mean under “textbox”? There are at least 3 objects in MS Word documents that can be considered as textboxes:

  1. Text box shape. In this case you can use the following code:
import aspose.words as aw

doc = aw.Document("C:\\Temp\\in.docx")
# get shapes
for n in doc.get_child_nodes(aw.NodeType.SHAPE, True) :
    s = n.as_shape()
    if s.shape_type == aw.drawing.ShapeType.TEXT_BOX :
        # get text os the textbox shape
        print(s.to_string(aw.SaveFormat.TEXT).strip())
  1. Legacy input text form field:
import aspose.words as aw

doc = aw.Document("C:\\Temp\\in.docx")
for ff in doc.range.form_fields :
    if ff.type == aw.fields.FieldType.FIELD_FORM_TEXT_INPUT :
        print(ff.result)
  1. Structured document tag text field:
import aspose.words as aw

doc = aw.Document("C:\\Temp\\in.docx")
for sdt in doc.range.structured_document_tags :
    if sdt.sdt_type == aw.markup.SdtType.RICH_TEXT :
        print(sdt.to_string(aw.SaveFormat.TEXT).strip())

Thanks the 1st one was the one I was looking for,

1 Like