简体   繁体   中英

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."

在此处输入图片说明

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

import subprocess

PDFTOPPMPATH = r"C:\Program Files\poppler-0.68.0\bin\pdftoppm.exe"
PDFFILE = r"C:\Users\user\Desktop\CO880\Click_CLI\Wikibot\report(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.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM