是否可以使用Python在PDF中的某些区域进行OCR?

发布于 2025-02-01 13:44:57 字数 83 浏览 4 评论 0原文

是否可以使用Python在PDF中的某些区域进行OCR?我正在尝试构建一个程序,以从每个PDF表中提取一些信息,例如Autodesk Bim360做什么

Is it possible to perform an OCR in certain area in a PDF with python? I am trying to build a program to extract some information from every PDF sheet like what Autodesk BIM360 do

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2025-02-08 13:44:58

免责声明:我是Borb的作者(此答案中使用的库)

Borb是播放的开源,纯Python PDF库与其他著名的图书馆很好。例如,您可以插入pil图像,或matplotlib图表,您可以使用pytesseract将OCR应用于文档。

如果您想了解更多信息,则有一个大型示例存储库。您可以找到一个工作示例ocr 也是。

我将在此处重复此示例以使其完整性:

#!chapter_007/src/snippet_005.py
def main():

    # set up everything for OCR
    tesseract_data_dir: Path = Path("/home/joris/Downloads/tessdata-master/")
    assert tesseract_data_dir.exists()
    l: OCRAsOptionalContentGroup = OCRAsOptionalContentGroup(tesseract_data_dir)

    # read Document
    doc: typing.Optional[Document] = None
    with open("output_001.pdf", "rb") as pdf_file_handle:
        doc = PDF.loads(pdf_file_handle, [l])

    assert doc is not None

    # store Document
    with open("output_002.pdf", "wb") as pdf_file_handle:
        PDF.dumps(pdf_file_handle, doc)


if __name__ == "__main__":
    main()

这里发生的事情是OCR过程将其结果倾倒在PDF的无形层中(这些称为可选内容组)。因此,名称ocrasoptionalContentGroup

之后,您可以再次存储文档,它似乎包含文本,您ll可以搜索它,选择文本以及您曾经使用的所有内容。

disclaimer: I am the author of borb (the library used in this answer)

borb is an open-source, pure Python PDF library that plays nice with other famous libraries. e.g. you can insert PIL images, or matplotlib charts, and you can use pytesseract to apply OCR to your document.

There is a large examples repository in case you want to learn more. You can find a working example of OCR there as well.

I'll repeat the example here for completeness:

#!chapter_007/src/snippet_005.py
def main():

    # set up everything for OCR
    tesseract_data_dir: Path = Path("/home/joris/Downloads/tessdata-master/")
    assert tesseract_data_dir.exists()
    l: OCRAsOptionalContentGroup = OCRAsOptionalContentGroup(tesseract_data_dir)

    # read Document
    doc: typing.Optional[Document] = None
    with open("output_001.pdf", "rb") as pdf_file_handle:
        doc = PDF.loads(pdf_file_handle, [l])

    assert doc is not None

    # store Document
    with open("output_002.pdf", "wb") as pdf_file_handle:
        PDF.dumps(pdf_file_handle, doc)


if __name__ == "__main__":
    main()

What's happening here is that the OCR process is dumping its results in an invisible layer of the PDF (these are called optional content groups). Hence the name OCRAsOptionalContentGroup.

Afterwards, you can store the Document again, and it will appear to contain text, youll be able to search it, select the text, and everything youre used to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文