我开始在安装了 cygwin (Python 2.7) 的 Windows 7 x64 计算机上开发一个小型 pdf 到 jpg 脚本。以下效果完美:
import subprocess
filename = "test"
subprocess.check_output('gs -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)
在我的 Windows 10 x64 非 Cygwin 计算机 (Python 2.7) 上拉取此项目后,此代码出现错误,因为它不再将“gs”识别为 Ghostscript 的内置缩写。所以我尝试以下操作:
import subprocess
filename = "test"
subprocess.check_output('C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)
WindowsError: [Error 2] The system cannot find the file specified
好吧,我是初学者,在这里犯了一些明显的错误。还有"\b"
之间的字符.20\bin\
突出显示...向我表明我不知怎的还没有明确这是一条路径。
我也尝试过的事情(单独运行):
subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT')
subprocess.check_output("C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT/s", shell=True)
我读过了:
- 如何在Python中使用dir/s命令? https://stackoverflow.com/questions/15207271/how-to-use-the-dir-s-command-in-python
- 如何从 python 执行命令提示符命令 https://stackoverflow.com/questions/5486725/how-to-execute-a-command-prompt-command-from-python
- 用子进程包装 cmd.exe https://stackoverflow.com/questions/2028207/wrapping-cmd-exe-with-subprocess
- Python 子进程不接受正确的参数 https://stackoverflow.com/questions/26774488/python-subprocess-does-not-take-correct-arguments
- 用子进程包装 cmd.exe https://stackoverflow.com/questions/2028207/wrapping-cmd-exe-with-subprocess
无济于事
给出的错误始终是:
Traceback (most recent call last):
File "C:/Python/Project/projectx/manual_conversion/manual_conversion.py", line 16, in <module>
subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o ' + filename + '-2800-%03d.jpg ' + filename + '.pdf', stderr=subprocess.STDOUT)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 567, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 711, in __init__
errread, errwrite)
File "C:\Python\Python 2.7.12\lib\subprocess.py", line 959, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
我完全理解这是菜鸟的东西+切换到unix会让我的生活更轻松-但在我的公司环境中我完全没有这个选择。
我也尝试过:
subprocess.check_output(r'C:\Program Files\gs\gs9.20\bin\gwin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf')
因为我认为我的变量串联可能破坏了命令......但这再次产生了相同的错误。
编辑 2:将完整参数作为列表传递
subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe -sDEVICE=jpeg -dPDFFitPage -g2800x3620 -o 2800-%03d.jpg test.pdf'])
产生同样的错误。但是这个:
subprocess.check_output([r'C:\Program Files\gs\gs9.20\bin\gswin64c.exe'])
是否在 Windows 中启动 Ghostscript。谢谢@lit让我尝试一些事情,现在的新问题是如何将选项传递给subprocess
' check_output 可执行文件。
仍然让我困扰的是它在 Windows 系统上的工作有多简单,仅仅因为它安装了 cygwin,以某种方式修改了 cmd 命名空间以识别“gs”......