如何在python中使用shell命令将pdf转换为jpg?
这是我尝试过的代码:如何在python中使用shell命令将pdf转换为jpg?,python,shell,pdf,imagemagick,Python,Shell,Pdf,Imagemagick,这是我尝试过的代码: import os path = r'/Users/J2015/Desktop' order = 'convert -density 100 -colorspace rgb {} {}'.format(os.path.join(path, r'testfile.pdf') , os.path.join(path, r'testimage.jpg')) os.system(order) 但是,我收到以下错误: sh:convert:未找到命令 我希望使用ImageMag
import os
path = r'/Users/J2015/Desktop'
order = 'convert -density 100 -colorspace rgb {} {}'.format(os.path.join(path, r'testfile.pdf') , os.path.join(path, r'testimage.jpg'))
os.system(order)
但是,我收到以下错误:
sh:convert:未找到命令
我希望使用ImageMagic进行此转换。你能告诉我怎么了吗?
我需要读还是写才能得到最终的.jpg文件?是否可以将其扩展到几个.pdf格式的转换?试试这个
PDFTOJPGPATH = r"path of your converter pdftojpg.exe"
PDFFILE = "your pdf file.pdf"
import subprocess
subprocess.Popen('"%s" -jpg "%s" out' % (PDFTJPGPATH, PDFFILE))
您正在执行的命令开头缺少
magick
。
根据命令,命令是
magick convert…
因此您的代码应该是这样的
导入操作系统
path=r'/Users/J2015/Desktop'#这应该是pdf和图像文件的绝对路径,这里我只是把它作为问题的作者,以便于理解我提到的更改。
顺序='magick convert-density 100-colorspace rgb{}{}'。格式(os.path.join(path,r'testfile.pdf'),os.path.join(path,r'testimage.jpg'))
操作系统(订单)
错误实际上是shell告诉您命令转换未知
,即convert不是可识别的命令或可执行文件,这是真的,因为magick convert
是命令。您确定已正确安装Imagemagic吗?你能在python之外完成吗?即直接从shell?os。系统
已弃用,您需要shell=True
,请参阅我正在使用MacOS,文件位于桌面上@J2015你能直接通过shell完成吗?你能告诉我你在哪里找到了pdftojpg吗?我刚刚检查了poppler目录,但找不到任何pdftojpg.exe