stat:路径应为字符串,字节,os.PathLike或整数,而不是NoneType-refextract

时间:2020-06-26 22:22:51

标签: python

在我的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:状态:路径应为字符串,字节,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.

1 个答案:

答案 0 :(得分:1)

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

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