【问题标题】:stat: path should be string, bytes, os.PathLike or integer, not NoneType - refextractstat:路径应该是字符串、字节、os.PathLike 或整数,而不是 NoneType - refextract
【发布时间】:2020-06-26 22:22:51
【问题描述】:

在我的Python 项目中,我尝试使用refextract 解析pdf 文件中的一些数据,但我无法使用它的extract_references_from_file 功能。

我正在使用网站上提到的示例代码:

from refextract import extract_references_from_file
references = extract_references_from_file('C02-1025.pdf')
print(references[0])

得到这个错误;

TypeError: stat: path 应该是字符串、字节、os.PathLike 或整数,而不是 NoneType

我尝试过不同的方法来传递文件路径;

references = extract_references_from_file(r"F:\project\python\C02-1025.pdf")

references = extract_references_from_file("F:\\project\\python\\C02-1025.pdf")

但没有任何效果。

我使用的是 Python 3.7.2,它是 64 位的。

这里是错误的完整追溯:

Traceback (most recent call last):
  File "refext.py", line 16, in <module>
    references = extract_references_from_file(r"F:\project\python\C02-1025.pdf")
  File "C:\Users\Username\AppData\Local\Programs\Python\Python37\lib\site-packages\refextract\references\api.py", line 128, in extract_references_from_file
    docbody = get_plaintext_document_body(path)
  File "C:\Users\Username\AppData\Local\Programs\Python\Python37\lib\site-packages\refextract\references\engine.py", line 1412, in get_plaintext_document_body
    textbody = convert_PDF_to_plaintext(fpath, keep_layout)
  File "C:\Users\Username\AppData\Local\Programs\Python\Python37\lib\site-packages\refextract\documents\pdf.py", line 457, in convert_PDF_to_plaintext
    if not os.path.isfile(CFG_PATH_PDFTOTEXT):
  File "C:\Users\Username\AppData\Local\Programs\Python\Python37\lib\genericpath.py", line 30, in isfile
    st = os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

refextract 库依赖于pdftotext 命令行实用程序。但是当我尝试安装它时

pip install pdftotext

它给了我这个错误

 ERROR: Command errored out with exit status 1:
 command: 'c:\users\usernamem\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\USER~1\\AppData\\Local\\Temp\\pip-install-l_9a5zt6\\pdftotext\\setup.py'"'"'; __file__='"'"'C:\\Users\\USER~1\\AppData\\Local\\Temp\\pip-install-l_9a5zt6\\pdftotext\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\USER~1\AppData\Local\Temp\pip-record-gpha3woc\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\users\username\appdata\local\programs\python\python37\Include\pdftotext'
     cwd: C:\Users\USER~1\AppData\Local\Temp\pip-install-l_9a5zt6\pdftotext\
Complete output (11 lines):
WARNING: pkg-config not found--guessing at poppler version.
         If the build fails, install pkg-config and try again.
running install
running build
running build_ext
building 'pdftotext' extension
creating build
creating build\temp.win-amd64-3.7
creating build\temp.win-amd64-3.7\Release
cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MT -DPOPPLER_CPP_AT_LEAST_0_30_0=1 "-Ic:\users\username\appdata\local\programs\python\python37\include" "-Ic:\users\username\appdata\local\programs\python\python37\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.17134.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" /EHsc /Tppdftotext.cpp /Fobuild\temp.win-amd64-3.7\Release\pdftotext.obj -Wall
error: command 'cl.exe' failed: No such file or directory
----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\users\username\appdata\local\programs\python\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\USER~1\\AppData\\Local\\Temp\\pip-install-l_9a5zt6\\pdftotext\\setup.py'"'"'; __file__='"'"'C:\\Users\\USER~1\\AppData\\Local\\Temp\\pip-install-l_9a5zt6\\pdftotext\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\USER~1\AppData\Local\Temp\pip-record-gpha3woc\install-record.txt' --single-version-externally-managed --compile --install-headers 'c:\users\username\appdata\local\programs\python\python37\Include\pdftotext' Check the logs for full command output.

【问题讨论】:

  • 你能给出你的异常的完整追溯吗?这显然发生在库代码中的某个地方,但试图通过调用进行跟踪很棘手,并且回溯应该准确地拼出它。
  • 添加了回溯。

标签: python


【解决方案1】:

您使用的refextract 库依赖于pdftotext 命令行实用程序。它目前没有在您的系统上找到该程序,这会导致您描述的错误。 可能是一个错误,错误是如此模糊。有一些代码试图给出更好的错误消息,但在这种情况下不起作用。

在 Linux 上,pdftotext 通常由您的发行版提供。在 Windows 上,您通常需要自己安装它。它来自Xpdf tools package。您要么需要将可执行文件安装在系统的PATH 的某个位置,要么需要通过设置环境变量CFG_PATH_PDFTOTEXTrefextract 指向程序的位置。

【讨论】:

  • 是的,pdftotext 没有安装,但它在安装时给了我错误。我添加了带有问题的回溯的完整错误。
  • 您不需要pdftotext 模块,只需要命令行实用程序。我在答案中给出的链接有一个预构建的 Windows 版本,它可能更容易安装。查看模块的安装说明,它似乎将获取实际实用程序委托给anaconda 分发,该分发获取基于Xpdfpoppler 包。
猜你喜欢
  • 1970-01-01
  • 2021-05-04
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
  • 2020-03-25
  • 1970-01-01
相关资源
最近更新 更多