Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active May 4, 2024 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/f528be76773cd3f9b26076b527b0c55b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f528be76773cd3f9b26076b527b0c55b to your computer and use it in GitHub Desktop.
Split a Word Document into Multiple Documents in Python
import aspose.words as aw
# load Word document
doc = aw.Document("document.docx")
# extract range of pages
extractedPages = doc.extract_pages(3, 6)
# save pages as a separate document
extractedPages.save("split_by_page_range.docx")
import aspose.words as aw
# load Word document
doc = aw.Document("document.docx")
# get page count
pageCount = doc.page_count
# loop through pages
for page in range(0, pageCount):
# save each page as a separate document
extractedPage = doc.extract_pages(page, 1)
extractedPage.save(f"split_by_page_{page + 1}.docx")
import aspose.words as aw
# load Word document
doc = aw.Document("document.docx")
for i in range(0, doc.sections.count) :
# clone the section to split
section = doc.sections[i].clone()
# create an instance of Document class for new doucment
newDoc = aw.Document()
# clear the default sections
newDoc.sections.clear()
# inster section into new document
newSection = newDoc.import_node(section, True).as_section()
newDoc.sections.add(newSection)
# Save section as a separate document
newDoc.save(f"split_by_sections_{i}.docx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment