pytesseract.pytesseract.tesseract_cmd = 'E:\\Tesseract-OCR\\tesseract'
由于这不仅是可以导入的独立库,而是一个独立的程序,因此我无法将其作为“--add_data”参数传递给pyinstaller。那么我该如何制作一个单文件可执行文件呢?pytesseract.pytesseract.tesseract_cmd = 'E:\\Tesseract-OCR\\tesseract'
由于这不仅是可以导入的独立库,而是一个独立的程序,因此我无法将其作为“--add_data”参数传递给pyinstaller。那么我该如何制作一个单文件可执行文件呢?https://tesseract-ocr.github.io/tessdoc/Compiling.html#windows
安装 MS Visual Studio 15(带 c++) 和 vcpkg 并通过命令提示符执行以下操作之一:
64 位: vcpkg install tesseract:x64-windows-static
32 位: vcpkg install tesseract:x86-windows-static
tesseract 可执行文件将位于 PC 上的 vcpkg 文件夹中的几个子文件夹内。除此文件外,您还需要下载一个 .trainneddata 文件,并将其放置在名为“tessdata”的文件夹中,与 tesseract exe 在同一目录下。
创建 pyinstaller 规范文件并编辑 Analysis(binaries=[]) 部分以包括 tesseract 所在的文件夹路径(如果您不使用 tesseract 的子文件夹,则需要添加 tesseract.exe 和 tessdata 子文件夹)。我也更改了 inclide_binaries=True
运行 pyinstaller 并包括选项 --specpath 'yourspecfile.spec'
我尚未尝试在另一台电脑上运行它,因此还没有完全测试它是否按预期工作(我对编译 c++ 一无所知,可能还需要其他文件/链接以支持 tesseract,因为我只在构建 PC 上进行测试)
@Zstr33的回答是正确的,但缺少细节。下面的指导已在Windows 10 64位上进行了测试。官方编译指南链接:https://tesseract-ocr.github.io/tessdoc/Compiling.html#windows。
步骤:
然后,单击“个人组件”。
你可以添加其他组件,但这些组件是编译tesseract静态二进制文件所需的。此外,如果你不使用英语,请单击语言包选项卡并添加英语语言包,这对于vcpkg是必需的。
按照快速入门指南安装vcpkg,可以在这里找到:https://github.com/microsoft/vcpkg#getting-started。
导航到复制vcpkg目录的位置,或将其添加到路径中。然后运行:vcpkg install tesseract:x64-windows-static
(64位)或vcpkg install tesseract:x86-windows-static
(32位)。
转到放置tesseract目录的位置\tesseract_x64-windows-static\tools\tesseract
(64位),或者放置tesseract目录的位置\tesseract_x86-windows-static\tools\tesseract
(32位)。
--onefile
与pyinstaller一起使用。我使用以下命令使用Tesseract和EasyOCR构建了我的应用程序exe,希望这可以帮到你。
python -m PyInstaller --paths "fullpath-to-custom-libraries" --add-data "C:\Program Files\Tesseract-OCR;Tesseract-OCR" --collect-all easyocr --onedir -w main.py
https://github.com/jbarlow83/OCRmyPDF/issues/659#issuecomment-714479684
然后,在运行exe时,我仍然遇到了一个错误,缺少pikepdf._cpphelpers
要解决这个问题,只需要添加:
from pikepdf import _cpphelpers
按照这里描述的方式,在您的Python文件中:
如何在我的脚本导入模块pikepdf和pdfminer3时修复pyinstaller“没有模块命名……”错误?
我的Pyinstaller调用看起来像这样:
pyinstaller --onefile appname.py --paths="C:\python\anaconda3\envs\appname\Lib\site-packages" --additional-hooks-dir="C:\coding\appname\Hooks"
由于使用pyinstaller打包可能会非常麻烦,因此我采取了以下步骤:
因此,最终用户将同时拥有tesseract安装程序和tesseract。 使用外部安装程序,您可以拥有很多自由度,并且还可以玩弄路径变量。
我尝试了使用pyinstaller和ocrmypdf,但始终无法使其正常工作。最终我使用了Nuitka,从一开始就可以正常工作 :-)
可以使用类似以下的方式:
python -m nuitka --mingw64 --standalone --follow-imports yourapp.py
http://nuitka.net/doc/user-manual.html
这里已经有一个类似的答案了,只是找不到链接了。
[python] [tesseract] path
中选择一个。 - stovflpyinstaller
设置正确的环境路径到tesseract
的部分。你必须在目标计算机上或在你的Python程序中进行设置。 - stovfl