Skip to content

Commit

Permalink
ENH: Add Jupyter Notebook integration for PdfReader (#2375)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Dec 28, 2023
1 parent 195d82e commit a91e9f6
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,35 @@ def __init__(
elif password is not None:
raise PdfReadError("Not encrypted file")

def _repr_mimebundle_(
self,
include: Union[None, Iterable[str]] = None,
exclude: Union[None, Iterable[str]] = None,
) -> Dict[str, Any]:
"""
Integration into Jupyter Notebooks.
This method returns a dictionary that maps a mime-type to it's
representation.
See https://ipython.readthedocs.io/en/stable/config/integrating.html
"""
self.stream.seek(0)
pdf_data = self.stream.read()
data = {
"application/pdf": pdf_data,
}

if include is not None:
# Filter representations based on include list
data = {k: v for k, v in data.items() if k in include}

if exclude is not None:
# Remove representations based on exclude list
data = {k: v for k, v in data.items() if k not in exclude}

return data

@property
def pdf_header(self) -> str:
"""
Expand Down

0 comments on commit a91e9f6

Please sign in to comment.