suba
suba

Reputation: 175

How to convert all pdf files in a directory/folder to image python 3?

How to convert all pdf files in a directory/folder to image python 3? is there any alternative?

while running this code i m getting an error like

pdf2image.exceptions.PDFPageCountError: Unable to get page count. Syntax Warning : May not be a PDF file (continuing anyway) Syntax Error: Couldn't find trailer dictionary Syntax Error: Couldn't find trailer dictionary Syntax Error: Couldn't read xref table

I dono why please someone help me

from pdf2image import convert_from_path
import glob,os
import os, subprocess

pdf_dir = r"C:\\Users\\xxx\\Desktop\\folder1\\folder2\\"
os.chdir(pdf_dir)

for pdf_file in os.listdir(pdf_dir):
    pages = convert_from_path(pdf_file, 500)
    for page in pages:
        page.save(pdf_file[:-4] +".jpg", 'JPEG')

Upvotes: 2

Views: 5801

Answers (1)

Madhan Varadhodiyil
Madhan Varadhodiyil

Reputation: 2116

I think you've both JPG and PDF in the same directory. To iterate over only PDF files:

from pdf2image import convert_from_path
import glob,os
import os, subprocess

pdf_dir = r"C:\\Users\\xxx\\Desktop\\folder1\\folder2\\"
os.chdir(pdf_dir)

for pdf_file in glob.glob(os.path.join(pdf_dir, "*.pdf")):
    pages = convert_from_path(pdf_file, 500)
    for page in pages:
        page.save(pdf_file[:-4] +".jpg", 'JPEG')

Upvotes: 6

Related Questions