Pages - Menu

Tuesday, April 14, 2020

How to Convert PDF to JPG format using Python code


Here you can find the code to convert the PDF file to JPG format.

Its very-simple code and using pdf2image library we can achieve the easy conversion.

First Step:
Install the essential library
PIP install pdf2image

Add the below code:


import os,glob
import tempfile
from pdf2image import convert_from_path
from PIL import Image
import os, subprocess




def convert_pdf(file_pathoutput_path):
    pdf_dir = r"X:\\XXXXX\\PDF"
    output_path=r"X:XXXX\\JPG_Images\\"
    os.chdir(pdf_dir)

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

if __name__ == "__main__":
    sourpath="./PDF"
    savpath="./JPG_images/"
    convert_pdf(sourpath,savpath)


No comments:

Post a Comment