Windows10调用GPL Ghostscript 9.54.0报错exit code 1如何解决?
问题原因
你遇到的报错核心是subprocess.Popen的参数传递逻辑错误,具体错误点如下:
- 输入文件传参错误:当使用列表形式调用subprocess时,每个命令行参数需要作为列表的独立元素传入。你将3个输入PDF路径拼接为一个完整字符串传入,Ghostscript会把整个字符串识别为单个文件的名称,找不到对应文件就会触发不可恢复错误。
- 多余的引号嵌套:列表形式传参不需要手动给路径加引号,系统会自动处理路径中的空格、特殊字符,你手动添加的单引号会被识别为文件名的一部分,进一步导致文件匹配失败。
- 未捕获错误日志:Ghostscript的错误信息会输出到标准错误流(stderr),你仅配置了捕获标准输出(stdout),所以看不到具体的报错提示(比如"找不到指定文件"这类明确提示),只能拿到退出码1。
- 未等待进程执行完成:
subprocess.Popen是异步启动进程,你打印"Subprocess complete"时Ghostscript进程可能还在运行,容易出现逻辑时序问题。
修复后的代码
import subprocess import time yyyymmdd_hhmmss = time.strftime('%Y_%m_%d__%H_%M_%S') print("Time stamp: " + yyyymmdd_hhmmss) # 路径不加额外引号,每个路径是独立的字符串元素 inputPDFsList = [f"//someNetwork/T/Tools/single_page_pdfs/Page_{i}.pdf" for i in range(1,4)] outputPDF_Path = f"//someNetwork/T/Tools/CompilationPDF_{yyyymmdd_hhmmss}.pdf" print("Input-Pages list:") for p in inputPDFsList: print(p) print("Output-PDF path:") print(outputPDF_Path) print("Begin Ghostscript process...") # 构造参数列表:命令 + 参数 + 所有输入文件(每个文件是列表独立元素) gs_cmd = [ '//someNetwork/T/Tools/Ghostscript_Tools/GS_Install/gs9.54.0/bin/gswin64c', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4', '-dNOPAUSE', '-dBATCH', '-dQUIET', '-dDOPDFMARKS', "-dPDFSETTINGS=/prepress", "-sPageList=1", f"-sOutputFile={outputPDF_Path}" ] # 把所有输入文件添加到参数列表末尾 gs_cmd.extend(inputPDFsList) # 启动进程,捕获stdout和stderr,等待执行完成 proc = subprocess.Popen(gs_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = proc.communicate() # 打印错误信息方便排查 if proc.returncode != 0: print(f"Ghostscript执行失败,错误码:{proc.returncode}") print(f"错误信息:{stderr.decode('gbk', errors='ignore')}") else: print("PDF合并完成")
内容的提问来源于stack exchange,提问作者Kenneth Eggering
相关产品推荐
相关产品推荐

