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

Implement '__enter__' in PdfReader #2665

Closed
KevFlo opened this issue May 21, 2024 · 3 comments · Fixed by #2666
Closed

Implement '__enter__' in PdfReader #2665

KevFlo opened this issue May 21, 2024 · 3 comments · Fixed by #2666
Labels
PdfReader The PdfReader component is affected

Comments

@KevFlo
Copy link

KevFlo commented May 21, 2024

Explanation

Make PdfReader more pythonic by using the 'with' 'as' context.

Code Example

from PyPDF2 import PdfReader
total_text = ""

pdf_file_path = "example/path" # Replace with your PDF file path
with PdfReader(pdf_file_path) as reader:
    total_text = '\n\n'.join([page.extract_text() for page in reader.pages])

Error trace back

Traceback (most recent call last):
File "pdfotxt.py", line 21, in
with PdfReader(pdf_file_path) as reader:
AttributeError: enter

@pubpub-zz
Copy link
Collaborator

PyPDF2 is obsolete. Feature should be available in pypdf

@KevFlo
Copy link
Author

KevFlo commented May 21, 2024

Just installed and re-ran code with pypdf,
I get the same error as with PyPDF2.

Traceback (most recent call last):
  File "pdfotxt.py", line 22, in <module>
    with PdfReader(pdf_file_path) as reader:
AttributeError: __enter__ 

@pubpub-zz
Copy link
Collaborator

Can you propose a PR?

@stefan6419846 stefan6419846 added the PdfReader The PdfReader component is affected label May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PdfReader The PdfReader component is affected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants