pyinstaller打包python-docx报错 No such file or directory (default-header.xml)
2023-06-13 09:12:09 时间
环境
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)
相关文章
- pycharm打包python项目_Python怎么打包
- pycharm远程部署_树莓派python环境
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- python读取文件如何去除空格_python读取txt文件时怎么去掉空格
- python编程是什么-Python编程
- python人工智能学习笔记_[Python] 人工智能与自然语言处理学习笔记(1)[通俗易懂]
- python jieba库_Python jieba库的使用说明「建议收藏」
- Python入门系列(十)一篇学会python文件处理
- 手机python 3.8解释器_Python 3.8 解释器安装教程
- 【说站】python isdigit如何判断字符串
- python的内置函数(十一)、range()
- python正则表达式贪婪与非贪婪匹配
- Python文件名后缀_python获取目录下所有文件的文件名
- Python如何生成随机数_产生随机数的常用方法
- python机器学习库sklearn——朴素贝叶斯分类器[通俗易懂]
- 用Python绘制了若干张词云图,惊艳了所有人
- 【7】python_matplotlib 输出(保存)矢量图方法;画图时图例说明(legend)放到图像外侧;Python_matplotlib图例放在外侧保存时显示不完整问题解决
- Python类和对象概念_Python自学第七节
- Python基础语法-函数的错误处理-raise语句
- python-异常处理和错误调试-异步IO程序的调试方法(二)
- python版本升级及pip部署方法详解编程语言
- python闭包的作用详解编程语言
- 如何在Linux上将Python脚本设置为后台运行?(linux后台运行python)
- Python实现Oracle数据库连接(python连接oracle数据库)
- MySQL与Python的协同运行:一种全新的开发体验(mysql与python)
- Linux Python:开启互联网新时代(linux python)
- Python操作MySQL数据库的必备模块mysqlpython(mysql_python)
- Python实现的下载8000首儿歌的代码分享