-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Labels
is-featureA feature requestA feature request
Description
Very similarly to #3154 when using PdfReader accessing all keys and checking whether the file contains javascript, I also get a KeyError.
Environment
Which environment were you using when you encountered the problem?
$ python -m platform
Linux-6.11.0-19-generic-x86_64-with-glibc2.39
$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==5.4.0, crypt_provider=('local_crypt_fallback', '0.0.0'), PIL=11.1.0Code + PDF
This is a minimal, complete example that shows the issue:
(Big tanks to @noamkush with #3154 as I was struggling reproducing the issue with a simple pdf)
from pypdf import PdfWriter, PdfReader
from io import BytesIO
writer = PdfWriter()
writer.add_attachment("test.txt", b"content")
stream = BytesIO()
writer.write(stream)
reader = PdfReader(stream)
names = reader.root_object.get("/Names")
for e in names.keys(): # works
print(names)
for e in names: # ko
print(names)Traceback
This is the complete traceback I see:
{'/EmbeddedFiles': IndirectObject(6, 0, 135387032409136)}
Traceback (most recent call last):
File "/src/pypdf_e.py", line 11, in <module>
for e in names:
File "/src/.venv/lib/python3.12/site-packages/pypdf/generic/_base.py", line 393, in __getitem__
return self._get_object_with_check()[key] # type: ignore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
File "/src/.venv/lib/python3.12/site-packages/pypdf/generic/_data_structures.py", line 478, in __getitem__
return dict.__getitem__(self, key).get_object()
^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 0
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
is-featureA feature requestA feature request