zl程序教程

您现在的位置是:首页 >  后端

当前栏目

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

Python打包XML 报错 File or No Default
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)