pyinstaller打包python-docx报错 No such file or directory (default-header.xml)
pyinstaller,打包,python,docx,报错,No,such,file,or,directory,default,header,xml
2025-04-01 16:27:59 时间
环境
Python 3.6.8
pyinstaller 4.10
python-docx 0.8.11
注: 只针对于使用了页眉和页脚的docx (其它正文正常)
报错分析
两个报错是类似的. 都是路径问题, 按理说不应该, 因为打包前是正常的, 打包后也不应该出问题, 好在问题比较简单, 只是路径的拼接问题. 查看实际路径发现 docx下面没得parts.
由于最终不会使用到parts目录, 所以解决办法有两个.
报错1 (header的)
Traceback (most recent call last):
File "multiprocessing/process.py", line 258, in _bootstrap
File "multiprocessing/process.py", line 93, in run
File "inspection/work_inspection.py", line 418, in inspection
report_result.append(report_docx.run(c,data1_result,baseinfo,inspection_data_result,hostdata))
File "inspection/report_docx.py", line 337, in run
p = header.paragraphs[0]
File "docx/blkcntnr.py", line 59, in paragraphs
File "docx/section.py", line 322, in _element
File "docx/section.py", line 342, in _get_or_add_definition
File "docx/section.py", line 414, in _add_definition
File "docx/parts/document.py", line 35, in add_header_part
File "docx/parts/hdrftr.py", line 44, in new
File "docx/parts/hdrftr.py", line 53, in _default_header_xml
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEIFQ2mqT/docx/parts/../templates/default-header.xml'
报错2(footer的)
Traceback (most recent call last):
File "multiprocessing/process.py", line 258, in _bootstrap
File "multiprocessing/process.py", line 93, in run
File "inspection/work_inspection.py", line 418, in inspection
report_result.append(report_docx.run(c,data1_result,baseinfo,inspection_data_result,hostdata))
File "inspection/report_docx.py", line 344, in run
paragraph = footer.paragraphs[0]
File "docx/blkcntnr.py", line 59, in paragraphs
File "docx/section.py", line 322, in _element
File "docx/section.py", line 342, in _get_or_add_definition
File "docx/section.py", line 370, in _add_definition
File "docx/parts/document.py", line 29, in add_footer_part
File "docx/parts/hdrftr.py", line 22, in new
File "docx/parts/hdrftr.py", line 31, in _default_footer_xml
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEIPIKP9j/docx/parts/../templates/default-footer.xml'
解决办法
解决办法1(推荐)
找到报错的代码docx/parts/hdrftr.py的第53行和31行. 代码是一样的, 我就只演示一处了
@classmethod
def _default_header_xml(cls):
"""Return bytes containing XML for a default header part."""
path = os.path.join(
os.path.split(__file__)[0], '..', 'templates', 'default-header.xml'
)
with open(path, 'rb') as f:
xml_bytes = f.read()
return xml_bytes
显然就是这里的路径拼接问题了, 可以使用字符串替换. 所以只需要在with open上面加个path = path.replace('parts/../','')即可
@classmethod
def _default_header_xml(cls):
"""Return bytes containing XML for a default header part."""
path = os.path.join(
os.path.split(__file__)[0], '..', 'templates', 'default-header.xml'
)
path = path.replace('parts/../','')
with open(path, 'rb') as f:
xml_bytes = f.read()
return xml_bytes
解决办法2
既然差个路径, 那就创建个路径呗
和方法1差不多, 在with open前面加个 os.makedirs(os.path.split(__file__)0, exist_ok = True)
相关文章
- 使用python快速开发桌面小工具
- python爬取图床壁纸
- NCAR抛弃PyNCL后又一面向地球科学的Python项目
- Python 二进制,十进制,十六进制转换「建议收藏」
- Python 学生信息管理系统——文章中源码100%真实有效—–如何将类、初始化属性、模块、循环判断、静态方法等一系列知识点结合起来做一个项目「建议收藏」
- 每天五分钟学Python,数字和字符串的基本用法
- python实现矩阵转置的几种方法
- Python概述
- Python项目45-前后端分离Home主页及后台(开撸)
- python读取oss的psd并上传jpg
- pythoncharm注释快捷键_多行注释以什么开头
- 不止短信!教你用 Python 发送告警通知到微信
- Win10配置Airsim环境并设置Python通信
- Python 模板渲染库 yaml 和 jinja2 的实战经验分享
- 统计学的Python实现-016:变异系数
- 使用 setup.py 将 Python 库打包分发到 PyPI 踩坑指南
- 工具推荐|面向气候研究者的Python可视化工具
- python 画图–简单开始及折线图[通俗易懂]
- .app 域名发布了,我们可以使用 Python 做点什么?
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」