The PdfMerger Class

class PyPDF2.PdfMerger(strict=False, overwriteWarnings='deprecated')[source]

Bases: object

Initializes a PdfMerger object. PdfMerger merges multiple PDFs into a single PDF. It can concatenate, slice, insert, or any combination of the above.

See the functions merge() (or append()) and write() for usage information.

Parameters
  • strict (bool) – Determines whether user should be warned of all problems and also causes some correctable problems to be fatal. Defaults to True.

  • overwriteWarnings (bool) – Determines whether to override Python’s warnings.py module with a custom implementation (defaults to True). This attribute is deprecated and will be removed.

addBookmark(title, pagenum, parent=None, color=None, bold=False, italic=False, fit='/Fit', *args)[source]

Deprecated since version 1.28.0: Use add_bookmark() instead.

addMetadata(infos)[source]

Deprecated since version 1.28.0: Use add_metadata() instead.

addNamedDestination(title, pagenum)[source]

Deprecated since version 1.28.0: Use add_named_destionation() instead.

add_bookmark(title, pagenum, parent=None, color=None, bold=False, italic=False, fit='/Fit', *args)[source]

Add a bookmark to this PDF file.

Parameters
  • title (str) – Title to use for this bookmark.

  • pagenum (int) – Page number this bookmark will point to.

  • parent – A reference to a parent bookmark to create nested bookmarks.

  • color (tuple) – Color of the bookmark as a red, green, blue tuple from 0.0 to 1.0

  • bold (bool) – Bookmark is bold

  • italic (bool) – Bookmark is italic

  • fit (str) – The fit of the destination page. See addLink() for details.

add_metadata(infos)[source]

Add custom metadata to the output.

Parameters

infos (dict) – a Python dictionary where each key is a field and each value is your new metadata. Example: {u'/Title': u'My title'}

add_named_destionation(title, pagenum)[source]

Add a destination to the output.

Parameters
  • title (str) – Title to use

  • pagenum (int) – Page number this destination points at.

append(fileobj, bookmark=None, pages=None, import_bookmarks=True)[source]

Identical to the merge() method, but assumes you want to concatenate all pages onto the end of the file instead of specifying a position.

Parameters
  • fileobj – A File Object or an object that supports the standard read and seek methods similar to a File Object. Could also be a string representing a path to a PDF file.

  • bookmark (str) – Optionally, you may specify a bookmark to be applied at the beginning of the included file by supplying the text of the bookmark.

  • pages – can be a PageRange or a (start, stop[, step]) tuple to merge only the specified range of pages from the source document into the output document.

  • import_bookmarks (bool) – You may prevent the source document’s bookmarks from being imported by specifying this as False.

close()[source]

Shuts all file descriptors (input and output) and clears all memory usage.

findBookmark(bookmark, root=None)[source]

Deprecated since version 1.28.0: Use find_bookmark() instead.

find_bookmark(bookmark, root=None)[source]
merge(position, fileobj, bookmark=None, pages=None, import_bookmarks=True)[source]

Merges the pages from the given file into the output file at the specified page number.

Parameters
  • position (int) – The page number to insert this file. File will be inserted after the given number.

  • fileobj – A File Object or an object that supports the standard read and seek methods similar to a File Object. Could also be a string representing a path to a PDF file.

  • bookmark (str) – Optionally, you may specify a bookmark to be applied at the beginning of the included file by supplying the text of the bookmark.

  • pages – can be a PageRange or a (start, stop[, step]) tuple to merge only the specified range of pages from the source document into the output document.

  • import_bookmarks (bool) – You may prevent the source document’s bookmarks from being imported by specifying this as False.

setPageLayout(layout)[source]

Deprecated since version 1.28.0: Use set_page_layout() instead.

setPageMode(mode)[source]

Deprecated since version 1.28.0: Use set_page_mode() instead.

set_page_layout(layout)[source]

Set the page layout

Parameters

layout (str) – The page layout to be used

Valid layout arguments

/NoLayout

Layout explicitly not specified

/SinglePage

Show one page at a time

/OneColumn

Show one column at a time

/TwoColumnLeft

Show pages in two columns, odd-numbered pages on the left

/TwoColumnRight

Show pages in two columns, odd-numbered pages on the right

/TwoPageLeft

Show two pages at a time, odd-numbered pages on the left

/TwoPageRight

Show two pages at a time, odd-numbered pages on the right

set_page_mode(mode)[source]

Set the page mode.

Parameters

mode (str) – The page mode to use.

Valid mode arguments

/UseNone

Do not show outlines or thumbnails panels

/UseOutlines

Show outlines (aka bookmarks) panel

/UseThumbs

Show page thumbnails panel

/FullScreen

Fullscreen view

/UseOC

Show Optional Content Group (OCG) panel

/UseAttachments

Show attachments panel

write(fileobj)[source]

Writes all data that has been merged to the given output file.

Parameters

fileobj – Output file. Can be a filename or any kind of file-like object.