Nutika 打包 docx 库 出现 docx.opc.exceptions.PackageNotFoundError: Package not found at 问题

docx.opc.exceptions.PackageNotFoundError: Package not found at

重现

用以下命令打包exe:

nuitka --standalone --show-memory --show-progress --nofollow-imports --enable-plugin=pyqt5 --follow-import-to=utils,src --output-dir=out --windows-icon-from-ico=./termius_1.ico ui.py

在运行时碰见访问 docx 库的 Document() 函数创建空表时, 会提示无法找到 default.docx 模板文件。

解决思路

显然是打包后目录重定义, 导致无法找到原本库内的资源文件, 或者说打包程序在打包docx库时没有考虑这个东西。

大致思路即通过查一下 Nutika 的帮助, 找到 --include-data-dir= 参数, 也就是在打包后的运行目录中添加 data 文件夹。

其参数格式为:--include-data-dir=本地路径=映射路径

如这里想将本地 E:/Study/conda/Lib/site-packages/docx/templates 路径下的文件放到 软件根目录/docx/templates 下, 就这么写:

--include-data-dir=E:/Study/conda/Lib/site-packages/docx/templates=docx/templates

注意, 如果是打包成单文件, 其并不会额外多出一个 docx 文件夹, 而是会在 exe 内部实现, 而正常情况下会在软甲根目录下创建对应文件夹。

docx 库的模板文件一般都在 /docx/templates 中。

解决方案

在原本的命令中添加该参数即可。
打包成单文件, 且图标为 当前目录/termius_1.ico, 运行时不回显Shell(不显示命令提示符窗口, 且用CMD运行也无控制台输出信息)

nuitka --standalone --show-memory --show-progress --nofollow-imports --enable-plugin=pyqt5 --follow-import-to=utils,src --output-dir=out --windows-icon-from-ico=./termius_1.ico --include-data-dir=E:/Study/conda/Lib/site-packages/docx/templates=docx/templates --onefile --windows-disable-console ui.py
posted @ 2023-06-07 23:38  EdwinAze  阅读(623)  评论(0编辑  收藏  举报