docxcompose与PyInstaller打包后出现FileNotFoundError问题求助
Fix FileNotFoundError for docxcompose custom.xml in PyInstaller EXE
Root Cause
PyInstaller doesn’t automatically package the template files (custom.xml) that docxcompose depends on. When the compiled EXE runs, it extracts files to a temporary directory but misses these template assets, leading to the error.
Solutions
1. Use PyInstaller Command Line with --add-data
First, locate the templates folder in your docxcompose installation. Run this code in Python to get the exact path:
import docxcompose import os print(os.path.join(os.path.dirname(docxcompose.__file__), "templates"))
Then use the path in your PyInstaller command:
- Windows:
pyinstaller --add-data "C:\path\to\docxcompose\templates\*;docxcompose/templates/" your_script.py - macOS/Linux:
pyinstaller --add-data "/path/to/docxcompose/templates/*:docxcompose/templates/" your_script.py
2. Use a PyInstaller Spec File
- Generate a spec file for your project:
pyinstaller --name your_app_name your_script.py - Open the generated
your_app_name.specfile, find thedataslist, and add the template directory path:datas=[ # Replace the first path with your actual docxcompose templates directory ('C:\\path\\to\\docxcompose\\templates\\', 'docxcompose/templates/'), ], - Recompile using the spec file:
pyinstaller your_app_name.spec
Verification
After compiling, run the EXE again. The custom.xml file will now be present in the temporary directory (C:\Users\*user*\AppData\Local\Temp\_MEIXXXXXX\docxcompose\templates\), resolving the FileNotFoundError.
内容的提问来源于stack exchange,提问作者Ty W.
相关产品推荐
相关产品推荐

