The PdfReader Class
- class PyPDF2.PdfReader(stream, strict=False, warndest=None, overwriteWarnings='deprecated')[source]
Bases:
object
Initialize a PdfReader object.
This operation can take some time, as the PDF stream’s cross-reference tables are read into memory.
- Parameters
stream – 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.
strict (bool) – Determines whether user should be warned of all problems and also causes some correctable problems to be fatal. Defaults to
True
.warndest – Destination for logging warnings (defaults to
sys.stderr
). This will be removed in PyPDF2 2.0.overwriteWarnings (bool) – Determines whether to override Python’s
warnings.py
module with a custom implementation (defaults toTrue
). This will be removed in PyPDF2 2.0.0.
- cacheGetIndirectObject(generation, idnum)[source]
Deprecated since version 1.28.0: Use
cache_get_indirect_object()
instead.
- cacheIndirectObject(generation, idnum, obj)[source]
Deprecated since version 1.28.0: Use
cache_indirect_object()
instead.
- decrypt(password)[source]
When using an encrypted / secured PDF file with the PDF Standard encryption handler, this function will allow the file to be decrypted. It checks the given password against the document’s user password and owner password, and then stores the resulting decryption key if either password is correct.
It does not matter which password was matched. Both passwords provide the correct decryption key that will allow the document to be used with this library.
- Parameters
password (str) – The password to match.
- Returns
0
if the password failed,1
if the password matched the user password, and2
if the password matched the owner password.- Return type
int
- Raises
NotImplementedError – if document uses an unsupported encryption method.
- property flattenedPages
Deprecated since version 1.28.0.
Use the attribute
flattened_pages
instead.
- property flattened_pages
- getDestinationPageNumber(destination)[source]
Deprecated since version 1.28.0: Use
get_destination_page_number()
instead.
- getFields(tree=None, retval=None, fileobj=None)[source]
Deprecated since version 1.28.0: Use
get_fields()
instead.
- getFormTextFields()[source]
Deprecated since version 1.28.0: Use
get_form_text_fields()
instead.
- getIsEncrypted()[source]
Deprecated since version 1.28.0: Use
is_encrypted
instead.
- getNamedDestinations(tree=None, retval=None)[source]
Deprecated since version 1.28.0: Use
named_destinations
instead.
- getObject(indirectReference)[source]
Deprecated since version 1.28.0: Use
get_object()
instead.
- getOutlines(node=None, outlines=None)[source]
Deprecated since version 1.28.0: Use the property
outlines
instead.
- getPage(pageNumber)[source]
Deprecated since version 1.28.0: Use
reader.pages[page_number]
instead.
- getPageLayout()[source]
Deprecated since version 1.28.0: Use
page_layout
instead.
- getPageNumber(page)[source]
Deprecated since version 1.28.0: Use
get_page_number()
instead.
- getXmpMetadata()[source]
Deprecated since version 1.28.0: Use the attribute
xmp_metadata
instead.
- get_destination_page_number(destination)[source]
Retrieve page number of a given Destination object.
- Parameters
destination (Destination) – The destination to get page number.
- Returns
the page number or -1 if page not found
- Return type
int
- get_fields(tree=None, retval=None, fileobj=None)[source]
Extracts field data if this PDF contains interactive form fields. The tree and retval parameters are for recursive use.
- Parameters
fileobj – A file object (usually a text file) to write a report to on all interactive form fields found.
- Returns
A dictionary where each key is a field name, and each value is a
Field
object. By default, the mapping name is used for keys.- Return type
dict, or
None
if form data could not be located.
- get_form_text_fields()[source]
Retrieves form fields from the document with textual data (inputs, dropdowns)
- get_outlines(node=None, outlines=None)[source]
Deprecated since version 1.28.0: Use the property
outlines
instead.
- get_page_number(page)[source]
Retrieve page number of a given PageObject
- Parameters
page (PageObject) – The page to get page number. Should be an instance of
PageObject
- Returns
the page number or -1 if page not found
- Return type
int
- property isEncrypted
Deprecated since version 1.28.0.
Use
is_encrypted
instead.
- property is_encrypted
Read-only boolean property showing whether this PDF file is encrypted. Note that this property, if true, will remain true even after the
decrypt()
method is called.
- property metadata
Retrieve the PDF file’s document information dictionary, if it exists. Note that some PDF files use metadata streams instead of docinfo dictionaries, and these metadata streams will not be accessed by this function.
- Returns
the document information of this PDF file
- Return type
DocumentInformation
orNone
if none exists.
- property namedDestinations
Deprecated since version 1.28.0.
Use
named_destinations
instead.
- property named_destinations
A read-only dictionary which maps names to
Destinations
- property numPages
Deprecated since version 1.28.0.
Use
len(reader.pages)
instead.
- property outlines
Read-only property.
- property pageLayout
Deprecated since version 1.28.0.
Use
page_layout
instead.
- property page_layout
Get the page layout.
- Returns
Page layout currently being used.
- Return type
str
,None
if not specified
/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
Get the page mode.
- Returns
Page mode currently being used.
- Return type
str
,None
if not specified
/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
Read-only property that emulates a list of
Page
objects.
- readNextEndLine(stream, limit_offset=0)[source]
Deprecated since version 1.28.0: Use
read_next_end_line()
instead.
- readObjectHeader(stream)[source]
Deprecated since version 1.28.0: Use
read_object_header()
instead.
- property resolvedObjects
Deprecated since version 1.28.0.
Use the attribute
resolved_objects
instead.
- property xmpMetadata
Deprecated since version 1.28.0.
Use the attribute
xmp_metadata
instead.
- property xmp_metadata
XMP (Extensible Metadata Platform) data
- Returns
a
XmpInformation
instance that can be used to access XMP metadata from the document.- Return type
XmpInformation
orNone
if no metadata was found on the document root.
- property xrefIndex
Deprecated since version 1.28.0.
Use the attribute
xref_index
instead.