Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Apr 10, 2025 8:31 am

Hello,

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
User avatar

William.Zhang
 
Posts: 757
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc

cron