CalledProcessError: Command '['pdftotext', '-layout', 'coburns.pdf', '-']' returned non-zero exit status 1?

CalledProcessError: Command '['pdftotext', '-layout', 'coburns.pdf', '-']' returned non-zero exit status 1?

我在 运行 这部分代码时不断收到错误消息。我已尝试按照 subprocess.check_output 文档的建议实施 universal_newlines=True,但我遇到了同样的错误。为什么会发生这种情况,我该如何解决?

for filename in os.listdir(directory):
    if filename in new_list:
        pdf = filename
        output = subprocess.check_output(['pdftotext', '-layout', pdf, '-'], universal_newlines=True).decode()
        pages = output.strip('\f').split('\f')
        page = pages[-1]

错误信息:

---------------------------------------------------------------------------
CalledProcessError                        Traceback (most recent call last)
<ipython-input-153-adc4a58d7f21> in <module>
      9     if filename in new_list:
     10         pdf = filename
---> 11         output = subprocess.check_output(['pdftotext', '-layout', pdf, '-'], universal_newlines=True).decode()
     12         pages = output.strip('\f').split('\f')
     13         page = pages[-1]

/usr/lib/python3.8/subprocess.py in check_output(timeout, *popenargs, **kwargs)
    409         kwargs['input'] = '' if kwargs.get('universal_newlines', False) else b''
    410 
--> 411     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
    412                **kwargs).stdout
    413 

/usr/lib/python3.8/subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    510         retcode = process.poll()
    511         if check and retcode:
--> 512             raise CalledProcessError(retcode, process.args,
    513                                      output=stdout, stderr=stderr)
    514     return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '['pdftotext', '-layout', 'coburns.pdf', '-']' returned non-zero exit status 1.

如评论中所述,check_output实际上是在进程返回的代码不是0时引发CalledProcessError

这里这个代码是1,说明pdftotext打不开pdf,要么是不正确,要么就是找不到。

您正在使用 os.listdir,它会生成与 directory 参数相关的文件名,该参数不一定与 check_output 将在其中启动的当前工作目录相同默认 pdftotext

我可以看到三个解决方案: