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: str, fdata: Union[str, bytes]) None [source]
Deprecated since version 1.28.0: Use
add_attachment()
instead.
- addBlankPage(width: Optional[float] = None, height: Optional[float] = None) PyPDF2._page.PageObject [source]
Deprecated since version 1.28.0: Use
add_blank_page()
instead.
- addBookmark(title: str, pagenum: int, parent: typing.Union[None, PyPDF2.generic.TreeObject, PyPDF2.generic.IndirectObject] = None, color: typing.Optional[typing.Tuple[float, float, float]] = None, bold: bool = False, italic: bool = False, fit: typing_extensions.Literal[/Fit, /XYZ, /FitH, /FitV, /FitR, /FitB, /FitBH, /FitBV] = '/Fit', *args: typing.List[typing.Union[PyPDF2.generic.NumberObject, PyPDF2.generic.NullObject]]) PyPDF2.generic.IndirectObject [source]
Deprecated since version 1.28.0: Use
add_bookmark()
instead.
- addBookmarkDestination(dest: PyPDF2._page.PageObject, parent: Optional[PyPDF2.generic.TreeObject] = None) PyPDF2.generic.IndirectObject [source]
Deprecated since version 1.28.0: Use
add_bookmark_destination()
instead.
- addBookmarkDict(bookmark: Union[PyPDF2.generic.Bookmark, PyPDF2.generic.Destination], parent: Optional[PyPDF2.generic.TreeObject] = None) PyPDF2.generic.IndirectObject [source]
Deprecated since version 1.28.0: Use
add_bookmark_dict()
instead.
- addLink(pagenum: int, pagedest: int, rect: PyPDF2.generic.RectangleObject, border: typing.Optional[PyPDF2.generic.ArrayObject] = None, fit: typing_extensions.Literal[/Fit, /XYZ, /FitH, /FitV, /FitR, /FitB, /FitBH, /FitBV] = '/Fit', *args: typing.Union[PyPDF2.generic.NumberObject, PyPDF2.generic.NullObject]) None [source]
Deprecated since version 1.28.0: Use
add_link()
instead.
- addMetadata(infos: Dict[str, Any]) None [source]
Deprecated since version 1.28.0: Use
add_metadata()
instead.
- addNamedDestination(title: str, pagenum: int) PyPDF2.generic.IndirectObject [source]
Deprecated since version 1.28.0: Use
add_named_destination()
instead.
- addNamedDestinationObject(dest: PyPDF2.generic.PdfObject) PyPDF2.generic.IndirectObject [source]
Deprecated since version 1.28.0: Use
add_named_destination_object()
instead.
- addPage(page: PyPDF2._page.PageObject) None [source]
Deprecated since version 1.28.0: Use
add_page()
instead.
- addURI(pagenum: int, uri: int, rect: PyPDF2.generic.RectangleObject, border: Optional[PyPDF2.generic.ArrayObject] = None) None [source]
Deprecated since version 1.28.0: Use
add_uri()
instead.
- add_attachment(filename: str, data: Union[str, bytes]) None [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: Optional[float] = None, height: Optional[float] = None) PyPDF2._page.PageObject [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
- Raises
PageSizeNotDefinedError – if width and height are not defined and previous page does not exist.
- add_bookmark(title: str, pagenum: int, parent: typing.Union[None, PyPDF2.generic.TreeObject, PyPDF2.generic.IndirectObject] = None, color: typing.Optional[typing.Tuple[float, float, float]] = None, bold: bool = False, italic: bool = False, fit: typing_extensions.Literal[/Fit, /XYZ, /FitH, /FitV, /FitR, /FitB, /FitBH, /FitBV] = '/Fit', *args: typing.List[typing.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_bookmark_destination(dest: PyPDF2._page.PageObject, parent: Optional[PyPDF2.generic.TreeObject] = None) PyPDF2.generic.IndirectObject [source]
- add_bookmark_dict(bookmark: Union[PyPDF2.generic.Bookmark, PyPDF2.generic.Destination], parent: Optional[PyPDF2.generic.TreeObject] = None) PyPDF2.generic.IndirectObject [source]
- add_js(javascript: str) None [source]
Add Javascript which will launch upon opening this PDF.
- Parameters
javascript (str) – Your Javascript.
>>> output.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});") # Example: This will launch the print window when the PDF is opened.
- add_link(pagenum: int, pagedest: int, rect: PyPDF2.generic.RectangleObject, border: typing.Optional[PyPDF2.generic.ArrayObject] = None, fit: typing_extensions.Literal[/Fit, /XYZ, /FitH, /FitV, /FitR, /FitB, /FitBH, /FitBV] = '/Fit', *args: typing.Union[PyPDF2.generic.NumberObject, PyPDF2.generic.NullObject]) None [source]
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.
rect –
RectangleObject
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.
/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: 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.
- add_named_destination_object(dest: PyPDF2.generic.PdfObject) PyPDF2.generic.IndirectObject [source]
- add_page(page: PyPDF2._page.PageObject) None [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: int, uri: int, rect: PyPDF2.generic.RectangleObject, border: Optional[PyPDF2.generic.ArrayObject] = None) 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.
rect –
RectangleObject
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: PyPDF2._reader.PdfReader, after_page_append: Optional[Callable[[PyPDF2._page.PageObject], None]] = None) None [source]
Deprecated since version 1.28.0: Use
append_pages_from_reader()
instead.
- append_pages_from_reader(reader: PyPDF2._reader.PdfReader, after_page_append: Optional[Callable[[PyPDF2._page.PageObject], None]] = None) 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: PyPDF2._reader.PdfReader, after_page_append: Optional[Callable[[PyPDF2._page.PageObject], None]] = None) None [source]
Deprecated since version 1.28.0: Use
clone_document_from_reader()
instead.
- cloneReaderDocumentRoot(reader: PyPDF2._reader.PdfReader) None [source]
Deprecated since version 1.28.0: Use
clone_reader_document_root()
instead.
- clone_document_from_reader(reader: PyPDF2._reader.PdfReader, after_page_append: Optional[Callable[[PyPDF2._page.PageObject], None]] = None) 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: PyPDF2._reader.PdfReader) None [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: str, owner_pwd: Optional[str] = None, use_128bit: bool = True, permissions_flag: int = - 1) None [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() PyPDF2.generic.ArrayObject [source]
Deprecated since version 1.28.0: Use
get_named_dest_root()
instead.
- getObject(ido: PyPDF2.generic.IndirectObject) PyPDF2.generic.PdfObject [source]
Deprecated since version 1.28.0: Use
get_object()
instead.
- getOutlineRoot() PyPDF2.generic.TreeObject [source]
Deprecated since version 1.28.0: Use
get_outline_root()
instead.
- getPage(pageNumber: int) PyPDF2._page.PageObject [source]
Deprecated since version 1.28.0: Use
writer.pages[page_number]
instead.
- getPageLayout() Optional[typing_extensions.Literal[/NoLayout, /SinglePage, /OneColumn, /TwoColumnLeft, /TwoColumnRight, /TwoPageLeft, /TwoPageRight]] [source]
Deprecated since version 1.28.0: Use
page_layout
instead.
- getPageMode() Optional[typing_extensions.Literal[/UseNone, /UseOutlines, /UseThumbs, /FullScreen, /UseOC, /UseAttachments]] [source]
Deprecated since version 1.28.0: Use
page_mode
instead.
- getReference(obj: PyPDF2.generic.PdfObject) PyPDF2.generic.IndirectObject [source]
Deprecated since version 1.28.0: Use
get_reference()
instead.
- get_page(pageNumber: int) PyPDF2._page.PageObject [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
- insertBlankPage(width: Optional[decimal.Decimal] = None, height: Optional[decimal.Decimal] = None, index: int = 0) PyPDF2._page.PageObject [source]
Deprecated since version 1.28.0: Use
insertBlankPage()
instead.
- insertPage(page: PyPDF2._page.PageObject, index: int = 0) None [source]
Deprecated since version 1.28.0: Use
insert_page()
instead.
- insert_blank_page(width: Optional[decimal.Decimal] = None, height: Optional[decimal.Decimal] = None, index: int = 0) PyPDF2._page.PageObject [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
- Raises
PageSizeNotDefinedError – if width and height are not defined and previous page does not exist.
- insert_page(page: PyPDF2._page.PageObject, index: int = 0) None [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: Optional[typing_extensions.Literal[/NoLayout, /SinglePage, /OneColumn, /TwoColumnLeft, /TwoColumnRight, /TwoPageLeft, /TwoPageRight]]
Deprecated since version 1.28.0.
Use
page_layout
instead.
- property pageMode: Optional[typing_extensions.Literal[/UseNone, /UseOutlines, /UseThumbs, /FullScreen, /UseOC, /UseAttachments]]
Deprecated since version 1.28.0.
Use
page_mode
instead.
- property page_layout: Optional[typing_extensions.Literal[/NoLayout, /SinglePage, /OneColumn, /TwoColumnLeft, /TwoColumnRight, /TwoPageLeft, /TwoPageRight]]
Page layout property.
/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: Optional[typing_extensions.Literal[/UseNone, /UseOutlines, /UseThumbs, /FullScreen, /UseOC, /UseAttachments]]
Page mode property.
/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: List[PyPDF2._page.PageObject]
Property that emulates a list of
PageObject
- removeImages(ignoreByteStringObject: bool = False) None [source]
Deprecated since version 1.28.0: Use
remove_images()
instead.
- removeLinks() None [source]
Deprecated since version 1.28.0: Use
remove_links()
instead.
- removeText(ignoreByteStringObject: bool = False) None [source]
Deprecated since version 1.28.0: Use
remove_text()
instead.
- remove_images(ignore_byte_string_object: bool = False) None [source]
Remove images from this output.
- Parameters
ignore_byte_string_object (bool) – optional parameter to ignore ByteString Objects.
- remove_text(ignore_byte_string_object: bool = False) None [source]
Remove text from this output.
- Parameters
ignore_byte_string_object (bool) – optional parameter to ignore ByteString Objects.
- setPageLayout(layout: typing_extensions.Literal[/NoLayout, /SinglePage, /OneColumn, /TwoColumnLeft, /TwoColumnRight, /TwoPageLeft, /TwoPageRight]) None [source]
Deprecated since version 1.28.0: Use
page_layout
instead.
- setPageMode(mode: typing_extensions.Literal[/UseNone, /UseOutlines, /UseThumbs, /FullScreen, /UseOC, /UseAttachments]) None [source]
Deprecated since version 1.28.0: Use
page_mode
instead.
- set_page_mode(mode: typing_extensions.Literal[/UseNone, /UseOutlines, /UseThumbs, /FullScreen, /UseOC, /UseAttachments]) None [source]
Deprecated since version 1.28.0: Use
page_mode
instead.
- updatePageFormFieldValues(page: PyPDF2._page.PageObject, fields: Dict[str, Any], flags: int = 0) None [source]
Deprecated since version 1.28.0: Use
update_page_form_field_values()
instead.
- update_page_form_field_values(page: PyPDF2._page.PageObject, fields: Dict[str, Any], flags: int = 0) None [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: Union[_io.BytesIO, _io.BufferedReader, _io.BufferedWriter, _io.FileIO]) None [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.