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/
Last active
May 4, 2024 08:32
-
-
Save aspose-com-gists/f528be76773cd3f9b26076b527b0c55b to your computer and use it in GitHub Desktop.
Split a Word Document into Multiple Documents in Python
This file contains 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 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 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