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.

Wed Apr 15, 2026 8:06 am

I want to remove the first two paragraphs from each document in a folder of Word documents.

First I create an object of Document class
Code: Select all
document = Document()

and load the document.

I have tried (but failed) to find the paragraphs with variations of:

Code: Select all
document.Sections.get_Item(0).Paragraphs.get_Item(0)
document.Sections.get_Item(0).Paragraphs.get_Item(1)


Then I thought I would use 'Replace' to replace the paragraph with nothing.
Happy to use any approach that works.

paulstgeorge
 
Posts: 2
Joined: Wed Apr 15, 2026 7:53 am

Wed Apr 15, 2026 9:16 am

Hello,

Thank you for your letter.
Please refer to the code below to delete them. If you have any other questions, you can provide your testing documents so that we can investigate more accurately.
Code: Select all
document = Document()
document.LoadFromFile(inputFile)
document.Sections.get_Item(0).Paragraphs.RemoveAt(0)
document.Sections.get_Item(0).Paragraphs.RemoveAt(1)
document.SaveToFile(outputFile, FileFormat.Docx)
document.Close()

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1510
Joined: Wed Apr 25, 2018 3:20 am

Wed Apr 15, 2026 9:47 am

Thank you! Should it be this (see line 4) because after I have removed the first paragraph the next paragraph is now at index 0????

document = Document()
document.LoadFromFile(inputFile)
document.Sections.get_Item(0).Paragraphs.RemoveAt(0)
document.Sections.get_Item(0).Paragraphs.RemoveAt(0)
document.SaveToFile(outputFile, FileFormat.Docx)
document.Close()

paulstgeorge
 
Posts: 2
Joined: Wed Apr 15, 2026 7:53 am

Wed Apr 15, 2026 9:59 am

Hello,

Thanks for your feedback.
Yes, your understanding is correct. You can directly execute it twice. If there are any issues with the specific document results, we need to further analyze based on the document.
Code: Select all
document = Document()
document.LoadFromFile(inputFile)
section = document.Sections.get_Item(0) 
for _ in range(2):
    section.Paragraphs.RemoveAt(0)     
document.SaveToFile(outputFile, FileFormat.Docx)
document.Close()

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1510
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc