[subprocess.call()在python中尽管没有产生错误也没有产生文件

问题描述 投票:0回答:1

我在下面使用此脚本,摘自此处:https://github.com/theeko74/pdfc/blob/master/pdf_compressor.py

import os
import sys
import subprocess
def compress(input_file_path, output_file_path, power=0):
    """Function to compress PDF via Ghostscript command line interface"""
    quality = {
        0: '/default',
        1: '/prepress',
        2: '/printer',
        3: '/ebook',
        4: '/screen'
    }

    # Basic controls
    # Check if valid path
    if not os.path.isfile(input_file_path):
        print("Error: invalid path for input PDF file")
        sys.exit(1)

    # Check if file is a PDF by extension
    if input_file_path.split('.')[-1].lower() != 'pdf':
        print("Error: input file is not a PDF")
        sys.exit(1)

    print("Compress PDF...")
    initial_size = os.path.getsize(input_file_path)
    subprocess.Popen(['gs', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4',
                    '-dPDFSETTINGS={}'.format(quality[power]),
                    '-dNOPAUSE', '-dQUIET', '-dBATCH',
                    '-sOutputFile={}'.format(output_file_path),
                     input_file_path])

compress("D:/Documents/Pdf Handler/test.pdf","D:/Documents/Pdf Handler/testCompress.pdf")

我想用它来压缩PDF,但是无论何时运行,都会产生以下错误:

Traceback (most recent call last):
  File "D:/Documents/Pdf Handler/compress.py", line 39, in <module>
    compress("D:/Documents/Pdf Handler/test.pdf","D:/Documents/Pdf Handler/testCompress.pdf")
  File "D:/Documents/Pdf Handler/compress.py", line 31, in compress
    input_file_path]
  File "C:\Python37\lib\subprocess.py", line 323, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Python37\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Python37\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

经过一些研究,我发现我必须在subprocess.call中添加shell = True:

...
    print("Compress PDF...")
    initial_size = os.path.getsize(input_file_path)
    subprocess.Popen(['gs', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4',
                    '-dPDFSETTINGS={}'.format(quality[power]),
                    '-dNOPAUSE', '-dQUIET', '-dBATCH',
                    '-sOutputFile={}'.format(output_file_path),
                     input_file_path])

compress("D:/Documents/Pdf Handler/test.pdf","D:/Documents/Pdf Handler/testCompress.pdf")

虽然这确实解决了引发错误的问题,但是代码现在似乎实际上并未执行任何操作。它运行,但是没有文件添加到指定目录。

您的帮助将不胜感激,正如我提到的,我的确从其他地方获取了此代码,并且文档非常稀疏。

python pdf subprocess
1个回答
0
投票

这不是真正的答案,但这是为了帮助您调试它。

您当前处于盲目状态,因此您需要像这样检查stdout和stderr的输出:

import subprocess

proc = subprocess.Popen(['gs', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4',
                         '-dPDFSETTINGS={}'.format(quality[power]),
                         '-dNOPAUSE', '-dQUIET', '-dBATCH',
                         '-sOutputFile={}'.format(output_file_path),
                        input_file_path],shell=True,stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE)
out, err = proc.communicate()
print(out)
print(err)
© www.soinside.com 2019 - 2024. All rights reserved.