I am working on a python project which requires me to loop through the multiple pdfs one by one stored in a folder called sample/ of my current directory and save the individual pages of those pdfs as images in another directory called converted_images/. Can someone help me? all the pdfs are named randomly but have a ".pdf" extension.
Asked
Active
Viewed 788 times
-2
-
Does this answer your question? [Extract a page from a pdf as a jpeg](https://stackoverflow.com/questions/46184239/extract-a-page-from-a-pdf-as-a-jpeg) – Victor Jan 20 '22 at 10:21
1 Answers
0
you can do it with pdf2image
pip install pdf2image
from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500)
for page in pages:
page.save('out.jpg', 'JPEG')
or:
import pypdfium2 as pdfium
pdffile = 'path/to/your_doc.pdf'
# render multiple pages concurrently (in this case: all)
for image, suffix in pdfium.render_pdf(pdffile):
image.save(f'output_{suffix}.jpg')
# render a single page (in this case: the first one)
with pdfium.PdfContext(pdffile) as pdf:
image = pdfium.render_page(pdf, 0)
image.save('output.jpg')

Tal Folkman
- 2,368
- 1
- 7
- 21