I am using pypdf I try to open pdf document . Pdf document cannot opened at error files as it throws WARNING:pypdf._reader:EOF marker not found

96 views Asked by At

To process pdf documents I am opening opening pdf document using

 input_pdf = PdfReader(open(input_pdf_path, "rb"))

In my case some files are not properly written so by manual opening itself the pdf document cannot be opened. it shows

WARNING:pypdf._reader:EOF marker not found

so i tried to catch that particular warnings so I can put that warning into exception

a) with warnings.catch_warnings(record=True) as warn_list:
b) input_pdf = PdfReader(open(input_pdf_path, "rb"))

# Process any warnings captured during the 'with' block
for warning_item in warn_list:
    # Check for the specific warning you want to catch
    if "EOF marker not found" in str(warning_item.message):
        print(f"Warning: {warning_item.message}")
        # Raise an exception to exit the function
        raise Exception("EOF marker not found warning")

But after it comes to b) line, it goes to a) line and pausing like still executing for a long time and not showing any improvement of executing next line code or raise exception .

I just want to catch execption and update it to the table as per my requirements

0

There are 0 answers