Skip to content

Instantly share code, notes, and snippets.

@btbytes
Forked from aspose-com-gists/readme.md
Created May 4, 2024 08:32
Show Gist options
  • Save btbytes/4f637af33a0dd062328dc29a482e0b30 to your computer and use it in GitHub Desktop.
Save btbytes/4f637af33a0dd062328dc29a482e0b30 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