我正在使用 OCRmyPDF 从扫描的 pdf 文件中提取文本。为此,我使用来自 this Colab notebook 的代码。唯一的区别是,我没有从在线 url 下载 pdf 文件,而是使用存储在本地机器上的 pdf 文件(将其替换为 {file_name} 而不是 {invoice_pdf})。在我运行之前,一切看起来都很好:
os.system(f'ocrmypdf {file_name} output.pdf')
我得到的不是 0,而是 512!下一行,当我运行 !ocrmypdf Performance Evaluations.pdf output.pdf
时,我收到一条无法识别的错误消息,内容如下:
usage: ocrmypdf [-h] [-l LANGUAGE] [--image-dpi DPI]
[--output-type {pdfa,pdf,pdfa-1,pdfa-2}] [--sidecar [FILE]]
[--version] [-j N] [-q] [-v [VERBOSE]] [--title TITLE]
[--author AUTHOR] [--subject SUBJECT] [--keywords KEYWORDS]
[-r] [--remove-background] [-d] [-c] [-i] [--oversample DPI]
[-f] [-s] [--skip-big MPixels] [--max-image-mpixels MPixels]
[--tesseract-config CFG] [--tesseract-pagesegmode PSM]
[--tesseract-oem MODE]
[--pdf-renderer {auto,tesseract,hocr,sandwich}]
[--tesseract-timeout SECONDS]
[--rotate-pages-threshold CONFIDENCE]
[--pdfa-image-compression {auto,jpeg,lossless}]
[--user-words FILE] [--user-patterns FILE] [--skip-repair]
[-k] [-g] [--flowchart FLOWCHART]
input_pdf_or_image output_pdf
ocrmypdf: error: unrecognized arguments: output.pdf
最后,运行以下行:
with pdfplumber.open('output.pdf') as pdf:
page = pdf.pages[0]
text = page.extract_text(x_tolerance=2)
print(text)
返回
FileNotFoundError Traceback (most recent call last)
<ipython-input-19-8274f7005856> in <module>()
----> 1 with pdfplumber.open('output.pdf') as pdf:
2 page = pdf.pages[0]
3 text = page.extract_text(x_tolerance=2)
4 print(text)
/usr/local/lib/python3.6/dist-packages/pdfplumber/pdf.py in open(cls, path_or_fp, **kwargs)
56 def open(cls, path_or_fp, **kwargs):
57 if isinstance(path_or_fp, (str, pathlib.Path)):
---> 58 fp = open(path_or_fp, "rb")
59 inst = cls(fp, **kwargs)
60 inst.close = fp.close
FileNotFoundError: [Errno 2] No such file or directory: 'output.pdf'
感谢任何帮助。谢谢
答案 0 :(得分:1)
如果文件名包含空格,则需要用引号将文件名括起来。
ocrmypdf "Performance Evaluations.pdf" output.pdf
或
ocrmypdf 'Performance Evaluations.pdf' output.pdf