Skip to content

Implement '__enter__' in PdfReader #2665

@KevFlo

Description

@KevFlo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    PdfReaderThe PdfReader component is affected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions