Thanks for your inquiry.
Do you want to get the section corresponding to the keyword? If yes, please refer to the code below for testing. If no, please write back and tell us more details about your needs. Some screenshots would be helpful. Thanks in advance.
- Code: Select all
# Specify the input and output file paths
inputFile = "Sample.docx"
# Create an object of the Document class
document = Document()
# Load a Word document
document.LoadFromFile(inputFile)
# Find all instances of a specific text
textSelections = document.FindAllString("Spire.Doc", False, True)
# Loop through all the instances
for selection in textSelections:
textRange = selection.GetAsOneRange()
# Get the section where the keyword is located
sec = textRange.OwnerParagraph.OwnerTextBody.Owner
# Get section index
secIndex = document.Sections.IndexOf(sec)
print(secIndex)
Sincerely,
William
E-iceblue support team