The PdfFileMerger Class

class PyPDF2.PdfFileMerger(strict=True)

Initializes a PdfFileMerger object. PdfFileMerger 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.
addBookmark(title, pagenum, parent=None)

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.
addMetadata(infos)

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'}
addNamedDestination(title, pagenum)

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)

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 Page Range 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()

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

merge(position, fileobj, bookmark=None, pages=None, import_bookmarks=True)

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 Page Range 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)

Set the page layout

Parameters:layout (str) – The page layout to be used
Valid layouts are:
/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
setPageMode(mode)

Set the page mode.

Parameters:mode (str) – The page mode to use.
Valid modes are:
/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)

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.