You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Generate a spec file for your project:
    pyinstaller --name your_app_name your_script.py
    
  2. Open the generated your_app_name.spec file, find the datas list, and add the template directory path:
    datas=[
        # Replace the first path with your actual docxcompose templates directory
        ('C:\\path\\to\\docxcompose\\templates\\', 'docxcompose/templates/'),
    ],
    
  3. 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.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 08:12:36