GroupDocs.Merger for Python via .NET can build a new document from chosen pages of several documents, in the order you list them: for example, page 3 of document A, then page 2 of document B, then page 1 of document A.
To join a whole document with chosen pages of other documents, pass PageJoinOptions to join, for example merger.join("./input2.pdf", PageJoinOptions([1, 3])).
To take any pages of any of the documents, in any order, use a PageBuilder, as this page shows.
A PageBuilder comes from merger.create_page_builder(). Its add_page method appends one page and its add_page_range method appends several. Once every page is listed, merger.apply_page_builder() makes them the document that save writes.
Note
PageBuilder works with paged formats such as PDF, Word processing documents and multi-page TIFF images. It is not available after a join with ImageJoinOptions, and while a spreadsheet joined with SpreadsheetJoinMode.ROWS is pending: in both cases create_page_builder or apply_page_builder raises GroupDocsMergerException (from groupdocs.merger.exceptions).
Specifying the pages by their page information
The easiest way to name a page is to pass its page information, which says which document the page comes from and its number. The builder’s documents list holds one entry per loaded document, and each entry’s pages list holds its pages.
Create a Merger with the path to the first document.
Call merger.join() to add each other document.
Call merger.create_page_builder() to get a PageBuilder.
Call add_page with each page you want, in order. The documents and pages lists count from 0: documents[0].pages[0] is page 1 of the first document.
Call merger.apply_page_builder() with the builder.
Call merger.save() with the path of the result.
fromgroupdocs.mergerimportMergerdefmerge_pages_in_order_by_page():withMerger("./input.docx")asmerger:merger.join("./input2.docx")page_builder=merger.create_page_builder()first=page_builder.documents[0]# the document the Merger openedsecond=page_builder.documents[1]# the first document joinedpage_builder.add_page(first.pages[0])# page 1 of the first documentpage_builder.add_page(second.pages[2])# page 3 of the second documentpage_builder.add_page(first.pages[1])# page 2 of the first documentpage_builder.add_page(second.pages[1])# page 2 of the second documentpage_builder.add_page(second.pages[0])# page 1 of the second documentpage_builder.add_page(first.pages[2])# page 3 of the first document# Make the listed pages the document to savemerger.apply_page_builder(page_builder)merger.save("./output.docx")if__name__=="__main__":merge_pages_in_order_by_page()
input.docx is a sample file used in this example. Click here to download it.
input2.docx is a sample file used in this example. Click here to download it.
Specifying the pages by document index and page number
Listing pages through documents and pages needs the information of every document loaded first. With many pages, it is faster to skip loading that information and name each page by the index of its document and its page number.
Create a Merger with the path to the first document, and call merger.join() to add each other document.
Call merger.create_page_builder() with the options.
Call add_page with the index of the document (0 is the document the Merger opened, 1 the first one joined) and the page number (1 is the first page), for each page in order.
Call merger.apply_page_builder() with the builder, then merger.save().
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportPageBuilderOptionsdefmerge_pages_in_order_by_number():withMerger("./input.docx")asmerger:merger.join("./input2.docx")# Do not load the documents' information: the pages are named by numberpage_builder_options=PageBuilderOptions()page_builder_options.load_document_info=Falsepage_builder=merger.create_page_builder(page_builder_options)# add_page(document index, page number): document 0 is the one the Merger openedpage_builder.add_page(0,1)# page 1 of the first documentpage_builder.add_page(1,3)# page 3 of the second documentpage_builder.add_page(0,2)# page 2 of the first documentpage_builder.add_page(1,2)# page 2 of the second documentpage_builder.add_page(1,1)# page 1 of the second documentpage_builder.add_page(0,3)# page 3 of the first documentmerger.apply_page_builder(page_builder)merger.save("./output.docx")if__name__=="__main__":merge_pages_in_order_by_number()
input.docx is a sample file used in this example. Click here to download it.
input2.docx is a sample file used in this example. Click here to download it.
add_page_range appends several pages in one call. It takes a list of page information, in the order the pages should appear.
Create a Merger with the path to the first document, and call merger.join() to add each other document.
Call merger.create_page_builder() to get a PageBuilder.
Call add_page_range with a list of the pages you want, taken from the documents and pages lists, which count from 0.
Call merger.apply_page_builder() with the builder, then merger.save().
fromgroupdocs.mergerimportMergerdefmerge_pages_in_order_as_range():withMerger("./input.docx")asmerger:merger.join("./input2.docx")page_builder=merger.create_page_builder()first=page_builder.documents[0]second=page_builder.documents[1]# Append the pages in the order they are listedpage_builder.add_page_range([first.pages[0],# page 1 of the first documentsecond.pages[2],# page 3 of the second documentfirst.pages[1],# page 2 of the first documentsecond.pages[1],# page 2 of the second documentsecond.pages[0],# page 1 of the second documentfirst.pages[2],# page 3 of the first document])merger.apply_page_builder(page_builder)merger.save("./output.docx")if__name__=="__main__":merge_pages_in_order_as_range()
input.docx is a sample file used in this example. Click here to download it.
input2.docx is a sample file used in this example. Click here to download it.