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

Raise KeyError: /Parent when attempting to extract the text from an empty page using layout mode #2533

Closed
amznero opened this issue Mar 22, 2024 · 1 comment · Fixed by #2555
Labels
key-error Could be a bug, but also a robustness issue workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow

Comments

@amznero
Copy link

amznero commented Mar 22, 2024

pypdf will raise a KeyError: /Parent when attempting to extract the text from an empty page using layout mode. (Default mode works fine)

Environment

ubuntu 22.04 + py3.10 + pypdf4.1.0

$ python test.py

Code + PDF

from pypdf import PdfReader
reader = PdfReader("test.pdf")
pages = reader.pages
for page in pages:
    text = page.extract_text()# works fine!
    text = page.extract_text(extraction_mode="layout")# raise KeyError

Share here the PDF file(s) that cause the issue. The smaller they are, the
better. Let us know if we may add them to our tests!

Traceback

This is the complete traceback I see:

Traceback (most recent call last):
  File "/mnt/d/codes/tmp/test.py", line 6, in <module>
    text = page.extract_text(extraction_mode="layout")# raise KeyError
  File "/home/hello/.local/lib/python3.10/site-packages/pypdf/_page.py", line 2050, in extract_text
    return self._layout_mode_text(
  File "/home/hello/.local/lib/python3.10/site-packages/pypdf/_page.py", line 1948, in _layout_mode_text
    fonts = self._layout_mode_fonts()
  File "/home/hello/.local/lib/python3.10/site-packages/pypdf/_page.py", line 1895, in _layout_mode_fonts
    objr = objr["/Parent"].get_object()


  File "/home/hello/.local/lib/python3.10/site-packages/pypdf/generic/_data_structures.py", line 319, in __getitem__
    return dict.__getitem__(self, key).get_object()
KeyError: '/Parent'

test.pdf

@stefan6419846 stefan6419846 added workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow key-error Could be a bug, but also a robustness issue labels Mar 22, 2024
@stefan6419846
Copy link
Collaborator

Thanks for the report. Do you want to submit a corresponding PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
key-error Could be a bug, but also a robustness issue workflow-text-extraction From a users perspective, text extraction is the affected feature/workflow
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants