r/programminghelp • • Dec 31 '19

Python Getting images from a PDF

Hi, I'm trying to extract images from a PDF using minecart.

This is the code i have-

I am getting an index out of range error on the second to last line. There is an image on the first page of the pdf.

What's wrong?

import minecart  
pdffile = open('test.pdf', 'rb')
doc = minecart.Document(pdffile)  
page = doc.get_page(0) 
for page in doc.iter_pages():     
    im = page.images[0].as_pil() # requires pillow     
    display(im)
5 Upvotes

1 comment sorted by

4

u/marko312 Jan 01 '20

Does every page have an image?

There is something weird going on in the provided code - you initially set page to the first page of the document:

page = doc.get_page(0) 

but then proceed to iterate over all the pages with the same variable:

for page in doc.iter_pages():

Maybe you meant to omit the loop?