The PdfWriter Class

class PyPDF2.PdfWriter[source]

Bases: object

This class supports writing PDF files out, given pages produced by another class (typically PdfReader).

addAttachment(fname, fdata)[source]

Deprecated since version 1.28.0: Use add_attachment() instead.

addBlankPage(width=None, height=None)[source]

Deprecated since version 1.28.0: Use add_blank_page() instead.

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.

addBookmarkDestination(dest, parent=None)[source]

Deprecated since version 1.28.0: Use add_bookmark_destination() instead.

addBookmarkDict(bookmark, parent=None)[source]

Deprecated since version 1.28.0: Use add_bookmark_dict() instead.

addJS(javascript)[source]

Deprecated since version 1.28.0: Use add_js() instead.

Deprecated since version 1.28.0: Use add_link() 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_destination() instead.

addNamedDestinationObject(dest)[source]

Deprecated since version 1.28.0: Use add_named_destination_object() instead.

addPage(page)[source]

Deprecated since version 1.28.0: Use add_page() instead.

addURI(pagenum, uri, rect, border=None)[source]

Deprecated since version 1.28.0: Use add_uri() instead.

add_attachment(filename, data)[source]

Embed a file inside the PDF.

Parameters
  • filename (str) – The filename to display.

  • data (str) – The data in the file.

Reference: https://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf Section 7.11.3

add_blank_page(width=None, height=None)[source]

Append a blank page to this PDF file and returns it. If no page size is specified, use the size of the last page.

Parameters
  • width (float) – The width of the new page expressed in default user space units.

  • height (float) – The height of the new page expressed in default user space units.

Returns

the newly appended page

Return type

PageObject

Raises

PageSizeNotDefinedError – if width and height are not defined and previous page does not exist.

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_bookmark_destination(dest, parent=None)[source]
add_bookmark_dict(bookmark, parent=None)[source]
add_js(javascript)[source]

Add Javascript which will launch upon opening this PDF.

Parameters

javascript (str) – Your Javascript.

>>> output.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
# Example: This will launch the print window when the PDF is opened.

Add an internal link from a rectangular area to the specified page.

Parameters
  • pagenum (int) – index of the page on which to place the link.

  • pagedest (int) – index of the page to which the link should go.

  • rectRectangleObject or array of four integers specifying the clickable rectangular area [xLL, yLL, xUR, yUR], or string in the form "[ xLL yLL xUR yUR ]".

  • border – if provided, an array describing border-drawing properties. See the PDF spec for details. No border will be drawn if this argument is omitted.

  • fit (str) – Page fit or ‘zoom’ option (see below). Additional arguments may need to be supplied. Passing None will be read as a null value for that coordinate.

Valid zoom arguments (see Table 8.2 of the PDF 1.7 reference for details)

/Fit

No additional arguments

/XYZ

[left] [top] [zoomFactor]

/FitH

[top]

/FitV

[left]

/FitR

[left] [bottom] [right] [top]

/FitB

No additional arguments

/FitBH

[top]

/FitBV

[left]

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.

add_named_destination(title, pagenum)[source]
add_named_destination_object(dest)[source]
add_page(page)[source]

Add a page to this PDF file. The page is usually acquired from a PdfReader instance.

Parameters

page (PageObject) – The page to add to the document. Should be an instance of PageObject

add_uri(pagenum, uri, rect, border=None)[source]

Add an URI from a rectangular area to the specified page. This uses the basic structure of AddLink

Parameters
  • pagenum (int) – index of the page on which to place the URI action.

  • uri (int) – string – uri of resource to link to.

  • rectRectangleObject or array of four integers specifying the clickable rectangular area [xLL, yLL, xUR, yUR], or string in the form "[ xLL yLL xUR yUR ]".

  • border – if provided, an array describing border-drawing properties. See the PDF spec for details. No border will be drawn if this argument is omitted.

REMOVED FIT/ZOOM ARG -John Mulligan

appendPagesFromReader(reader, after_page_append=None)[source]

Deprecated since version 1.28.0: Use append_pages_from_reader() instead.

append_pages_from_reader(reader, after_page_append=None)[source]

Copy pages from reader to writer. Includes an optional callback parameter which is invoked after pages are appended to the writer.

Parameters
  • reader – a PdfReader object from which to copy page annotations to this writer object. The writer’s annots will then be updated

  • reference) (writer_pageref (PDF page) – Reference to the page appended to the writer.

Callback after_page_append (function)

Callback function that is invoked after each page is appended to the writer. Callback signature:

cloneDocumentFromReader(reader, after_page_append=None)[source]

Deprecated since version 1.28.0: Use clone_document_from_reader() instead.

cloneReaderDocumentRoot(reader)[source]

Deprecated since version 1.28.0: Use clone_reader_document_root() instead.

clone_document_from_reader(reader, after_page_append=None)[source]

Create a copy (clone) of a document from a PDF file reader

Parameters

reader – PDF file reader instance from which the clone should be created.

Callback after_page_append (function)

Callback function that is invoked after each page is appended to the writer. Signature includes a reference to the appended page (delegates to appendPagesFromReader). Callback signature:

param writer_pageref (PDF page reference)

Reference to the page just appended to the document.

clone_reader_document_root(reader)[source]

Copy the reader document root to the writer.

Parameters

reader – PdfReader from the document root should be copied.

Callback after_page_append

encrypt(user_pwd, owner_pwd=None, use_128bit=True, permissions_flag=- 1)[source]

Encrypt this PDF file with the PDF Standard encryption handler.

Parameters
  • user_pwd (str) – The “user password”, which allows for opening and reading the PDF file with the restrictions provided.

  • owner_pwd (str) – The “owner password”, which allows for opening the PDF files without any restrictions. By default, the owner password is the same as the user password.

  • use_128bit (bool) – flag as to whether to use 128bit encryption. When false, 40bit encryption will be used. By default, this flag is on.

  • permissions_flag (unsigned int) – permissions as described in TABLE 3.20 of the PDF 1.7 specification. A bit value of 1 means the permission is grantend. Hence an integer value of -1 will set all flags. Bit position 3 is for printing, 4 is for modifying content, 5 and 6 control annotations, 9 for form fields, 10 for extraction of text and graphics.

getNamedDestRoot()[source]

Deprecated since version 1.28.0: Use get_named_dest_root() instead.

getNumPages()[source]

Deprecated since version 1.28.0: Use len(writer.pages) instead.

getObject(ido)[source]

Deprecated since version 1.28.0: Use get_object() instead.

getOutlineRoot()[source]

Deprecated since version 1.28.0: Use get_outline_root() instead.

getPage(pageNumber)[source]

Deprecated since version 1.28.0: Use writer.pages[page_number] instead.

getPageLayout()[source]

Deprecated since version 1.28.0: Use page_layout instead.

getPageMode()[source]

Deprecated since version 1.28.0: Use page_mode instead.

getReference(obj)[source]

Deprecated since version 1.28.0: Use get_reference() instead.

get_named_dest_root()[source]
get_object(ido)[source]
get_outline_root()[source]
get_page(page_umber)[source]

Retrieve a page by number from this PDF file.

Parameters

pageNumber (int) – The page number to retrieve (pages begin at zero)

Returns

the page at the index given by pageNumber

Return type

PageObject

get_reference(obj)[source]
insertBlankPage(width=None, height=None, index=0)[source]

Deprecated since version 1.28.0: Use insertBlankPage() instead.

insertPage(page, index=0)[source]

Deprecated since version 1.28.0: Use insert_page() instead.

insert_blank_page(width=None, height=None, index=0)[source]

Insert a blank page to this PDF file and returns it. If no page size is specified, use the size of the last page.

Parameters
  • width (float) – The width of the new page expressed in default user space units.

  • height (float) – The height of the new page expressed in default user space units.

  • index (int) – Position to add the page.

Returns

the newly appended page

Return type

PageObject

Raises

PageSizeNotDefinedError – if width and height are not defined and previous page does not exist.

insert_page(page, index=0)[source]

Insert a page in this PDF file. The page is usually acquired from a PdfReader instance.

Parameters
  • page (PageObject) – The page to add to the document. This argument should be an instance of PageObject.

  • index (int) – Position at which the page will be inserted.

property pageLayout

Deprecated since version 1.28.0.

Use page_layout instead.

property pageMode

Deprecated since version 1.28.0.

Use page_mode instead.

property page_layout

Page layout property.

Valid layout values

/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

property page_mode

Page mode property.

Valid mode values

/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

property pages

Property that emulates a list of PageObject

removeImages(ignoreByteStringObject=False)[source]

Deprecated since version 1.28.0: Use remove_images() instead.

Deprecated since version 1.28.0: Use remove_links() instead.

removeText(ignoreByteStringObject=False)[source]

Deprecated since version 1.28.0: Use remove_text() instead.

remove_images(ignore_byte_string_object=False)[source]

Remove images from this output.

Parameters

ignoreByteStringObject (bool) – optional parameter to ignore ByteString Objects.

Remove links and annotations from this output.

remove_text(ignore_byte_string_object=False)[source]

Remove text from this output.

Parameters

ignoreByteStringObject (bool) – optional parameter to ignore ByteString Objects.

setPageLayout(layout)[source]

Deprecated since version 1.28.0: Use page_layout instead.

setPageMode(mode)[source]

Deprecated since version 1.28.0: Use page_mode instead.

set_need_appearances_writer()[source]
set_page_mode(mode)[source]

Deprecated since version 1.28.0: Use page_mode instead.

updatePageFormFieldValues(page, fields, flags=0)[source]

Deprecated since version 1.28.0: Use update_page_form_field_values() instead.

update_page_form_field_values(page, fields, flags=0)[source]

Update the form field values for a given page from a fields dictionary. Copy field texts and values from fields to page. If the field links to a parent object, add the information to the parent.

Parameters
  • page – Page reference from PDF writer where the annotations and field data will be updated.

  • fields – a Python dictionary of field names (/T) and text values (/V)

  • flags – An integer (0 to 7). The first bit sets ReadOnly, the second bit sets Required, the third bit sets NoExport. See PDF Reference Table 8.70 for details.

write(stream)[source]

Write the collection of pages added to this object out as a PDF file.

Parameters

stream – An object to write the file to. The object must support the write method and the tell method, similar to a file object.