pythonpdf2image

How can I fix this simple error using pdf2image?


Beginner here, not sure what I am doing wrong. Any help is greatly appreciated.

Simple Code:

from pdf2image import convert_from_path

images = convert_from_path('sample.pdf', 500, poppler_path=r'C:\Program Files\poppler-24.07.0\Library\bin')

for i in range(len(images)):
    images[i].save('page' + str(i) + '.jpg', 'JPEG')

Error:

pdf2image.exceptions.PDFPageCountError: Unable to get page count.
I/O Error: Couldn't open file 'sample.pdf': No error.

Originally, the module coundn't get a page count because I did not have a poppler. After I figured out the poppler, I thought it would work, but it still does not.


Solution

  • Usually, I have this problem because the path to the file is not defined correctly. That is why the lib is unable to open and read pdf. Try to give the correct path by copying it from the file: "copy path" or "copy relative path".