pdf2image
module in Python can
be used to convert pdf files to images. This post explains how to convert PDF
files to images using Python.
pdf2image
module requires
Poppler PDF rendering library. Let's set up Poppler for Windows.
1. Download Poppler from this Link.
2. Extract the downloaded binary and place the extracted folder in
C:\Program Files\
.
1. Install pdf2image
module.
pip install pdf2image
2. Import required module and function.
from pdf2image import convert_from_path
3. Define Poppler
executable path.
poppler_path = r'C:\Program Files\poppler-0.68.0\bin'
4. Define pdf file path.
pdf_path = "sample.pdf" # Replace with path of PDF File
5. Convert PDF file to images using convert_from_path
function.
images = convert_from_path(pdf_path=pdf_path, poppler_path=poppler_path)
6. Iterate over PDF pages and save each page as PNG image.
for count, img in enumerate(images):
img_name = f"page_{count}.png"
img.save(img_name, "PNG")
7. After successful execution you should see image for each PDF page in current working directory.
8. Complete code for converting PDF page to image
from pdf2image import convert_from_path
poppler_path = r'C:\Program Files\poppler-0.68.0\bin'
pdf_path = "sample.pdf"
images = convert_from_path(pdf_path=pdf_path, poppler_path=poppler_path)
for count, img in enumerate(images):
img_name = f"page_{count}.png"
img.save(img_name, "PNG")
Categories: Python
Similar Articles