pyinstaller打包python-docx报错 No such file or directory (default-header.xml)

pyinstaller打包python-docx报错 No such file or directory (default-header.xml)
发布于2022-09-07 15:15:19阅读 2410
环境
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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值