The PdfMerger Class

class PyPDF2.PdfMerger(strict: bool = False)[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 False.

addBookmark(title: str, pagenum: int, parent: Union[None, PyPDF2.generic.TreeObject, PyPDF2.generic.IndirectObject] = None, color: Optional[Tuple[float, float, float]] = None, bold: bool = False, italic: bool = False, fit: str = '/Fit', *args: Union[PyPDF2.generic.NumberObject, PyPDF2.generic.NullObject]) PyPDF2.generic.IndirectObject[source]

Deprecated since version 1.28.0: Use add_bookmark() instead.

addMetadata(infos: Dict[str, Any]) None[source]

Deprecated since version 1.28.0: Use add_metadata() instead.

addNamedDestination(title: str, pagenum: int) None[source]

Deprecated since version 1.28.0: Use add_named_destination() instead.

add_bookmark(title: str, pagenum: int, parent: Union[None, PyPDF2.generic.TreeObject, PyPDF2.generic.IndirectObject] = None, color: Optional[Tuple[float, float, float]] = None, bold: bool = False, italic: bool = False, fit: str = '/Fit', *args: Union[PyPDF2.generic.NumberObject, PyPDF2.generic.NullObject]) PyPDF2.generic.IndirectObject[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: Dict[str, Any]) None[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_destination(title: str, pagenum: int) None[source]

Add a destination to the output.

Parameters
  • title (str) – Title to use

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

append(fileobj: Union[str, _io.BytesIO, _io.BufferedReader, _io.BufferedWriter, _io.FileIO, PyPDF2._reader.PdfReader], bookmark: Optional[str] = None, pages: Union[None, PyPDF2.pagerange.PageRange, Tuple[int, int], Tuple[int, int, int]] = None, import_bookmarks: bool = True) None[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() None[source]

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

find_bookmark(bookmark: Dict[str, Any], root: Optional[List[Union[PyPDF2.generic.Destination, List[Union[PyPDF2.generic.Destination, List[PyPDF2.generic.Destination]]]]]] = None) Optional[List[int]][source]
merge(position: int, fileobj: Union[str, _io.BytesIO, _io.BufferedReader, _io.BufferedWriter, _io.FileIO, PyPDF2._reader.PdfReader], bookmark: Optional[str] = None, pages: Optional[Union[str, PyPDF2.pagerange.PageRange, Tuple[int, int], Tuple[int, int, int]]] = None, import_bookmarks: bool = True) None[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: typing_extensions.Literal[/NoLayout, /SinglePage, /OneColumn, /TwoColumnLeft, /TwoColumnRight, /TwoPageLeft, /TwoPageRight]) None[source]

Deprecated since version 1.28.0: Use set_page_layout() instead.

setPageMode(mode: typing_extensions.Literal[/UseNone, /UseOutlines, /UseThumbs, /FullScreen, /UseOC, /UseAttachments]) None[source]

Deprecated since version 1.28.0: Use set_page_mode() instead.

set_page_layout(layout: typing_extensions.Literal[/NoLayout, /SinglePage, /OneColumn, /TwoColumnLeft, /TwoColumnRight, /TwoPageLeft, /TwoPageRight]) None[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: typing_extensions.Literal[/UseNone, /UseOutlines, /UseThumbs, /FullScreen, /UseOC, /UseAttachments]) None[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: Union[str, _io.BytesIO, _io.BufferedReader, _io.BufferedWriter, _io.FileIO]) None[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.