Learn how to split a Word document into multiple documents in Python: https://blog.aspose.com/2021/11/18/split-a-word-document-in-python/
-
-
Save btbytes/4f637af33a0dd062328dc29a482e0b30 to your computer and use it in GitHub Desktop.
Split a Word Document into Multiple Documents in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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