前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >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
7040
发布2022-09-07 15:15:19
举报
文章被收录于专栏:大大刺猬

环境

Python 3.6.8

pyinstaller 4.10

python-docx 0.8.11

注: 只针对于使用了页眉和页脚的docx (其它正文正常)

报错分析

两个报错是类似的. 都是路径问题, 按理说不应该, 因为打包前是正常的, 打包后也不应该出问题, 好在问题比较简单, 只是路径的拼接问题. 查看实际路径发现 docx下面没得parts.

由于最终不会使用到parts目录, 所以解决办法有两个.

报错1 (header的)

代码语言:txt
复制
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的)

代码语言:txt
复制
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行. 代码是一样的, 我就只演示一处了

代码语言:python
代码运行次数:0
复制
    @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/../','')即可

代码语言:python
代码运行次数:0
复制
    @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)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境
  • 报错分析
    • 报错1 (header的)
      • 报错2(footer的)
      • 解决办法
        • 解决办法1(推荐)
          • 解决办法2
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档