Skip to content
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

ENH: Add Jupyter Notebook integration for PdfReader #2375

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 @@
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 = {

Check warning on line 359 in pypdf/_reader.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_reader.py#L357-L359

Added lines #L357 - L359 were not covered by tests
"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

Check warning on line 371 in pypdf/_reader.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_reader.py#L371

Added line #L371 was not covered by tests

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