Skip to content

ENH: Add Jupyter Notebook integration for PdfReader #2375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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