Skip to content
Advertisement

how to extract 1st page after converting a pdf file to an image with subprocess.Popen

I’m trying to convert pdf files to images, and I’m doing it with subprocess. Now I need a way to extract only the first page without having to convert all of the images. In this case, for example, I only need to convert “out-1.png.”

enter image description here

What exactly do I need to get this done? Here’s the code I’m currently using:

import subprocess

PDFTOPPMPATH = r"C:Program Filespoppler-0.68.0binpdftoppm.exe"
PDFFILE = r"C:UsersuserDesktopCO880Click_CLIWikibotreport(7).pdf"
subprocess.Popen('"%s" -png "%s" out' % (PDFTOPPMPATH, PDFFILE))

Note: I have no intention to use pdf2image as I found a few errors with it.

Advertisement

Answer

After looking into the '"%s" -png "%s" out', I discovered that I can pass extra parameters to get the first page. The first parameter to pass is -f <int> which specifies the first page to convert; however, you must also pass -l <int> to specify the last page to convert. So eventually, I solved my problem by editing my last line to: subprocess.Popen('"%s" -png "%s" out -f 1 -l 1' % (PDFTOPPMPATH, PDFFILE))

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement