python-docx 报错 KeyError: “There is no item named ‘NULL‘ in the archive“

python-docx 报错 KeyError: "There is no item named 'NULL' in the archive"

    • 原因
    • 解决办法
      • [手动删除 docx 文件中的无效引用](#手动删除 docx 文件中的无效引用)
      • [删除 `Target="../NULL"` 的 `Relationship` 并重新打包为 docx](#删除 Target="../NULL"Relationship 并重新打包为 docx)
      • [解析 docx 时忽略 `Target="../NULL"` 的 `Relationship`](#解析 docx 时忽略 Target="../NULL"Relationship)
    • 参考
python 复制代码
import docx

doc = docx.Document('DE862197-1772524393022.docx')

在使用 python-docx 解析 .docx 文件时遇到以下的报错:

shell 复制代码
Traceback (most recent call last):
  File "D:\project\python\p1\t1.py", line 3, in <module>
    doc = docx.Document('DE862197-1772524393022.docx')
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\project\python\p1\.venv\Lib\site-packages\docx\api.py", line 27, in Document
    document_part = cast("DocumentPart", Package.open(docx).main_document_part)
                                         ^^^^^^^^^^^^^^^^^^
  File "D:\project\python\p1\.venv\Lib\site-packages\docx\opc\package.py", line 126, in open
    pkg_reader = PackageReader.from_file(pkg_file)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\project\python\p1\.venv\Lib\site-packages\docx\opc\pkgreader.py", line 25, in from_file
    sparts = PackageReader._load_serialized_parts(phys_reader, pkg_srels, content_types)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\project\python\p1\.venv\Lib\site-packages\docx\opc\pkgreader.py", line 51, in _load_serialized_parts
    for partname, blob, reltype, srels in part_walker:
                                          ^^^^^^^^^^^
  File "D:\project\python\p1\.venv\Lib\site-packages\docx\opc\pkgreader.py", line 82, in _walk_phys_parts
    for partname, blob, reltype, srels in next_walker:
                                          ^^^^^^^^^^^
  File "D:\project\python\p1\.venv\Lib\site-packages\docx\opc\pkgreader.py", line 79, in _walk_phys_parts
    blob = phys_reader.blob_for(partname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\project\python\p1\.venv\Lib\site-packages\docx\opc\phys_pkg.py", line 83, in blob_for
    return self._zipf.read(pack_uri.membername)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\software\uv\python\cpython-3.12.12-windows-x86_64-none\Lib\zipfile\__init__.py", line 1584, in read
    with self.open(name, "r", pwd) as fp:
         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\software\uv\python\cpython-3.12.12-windows-x86_64-none\Lib\zipfile\__init__.py", line 1621, in open
    zinfo = self.getinfo(name)
            ^^^^^^^^^^^^^^^^^^
  File "D:\software\uv\python\cpython-3.12.12-windows-x86_64-none\Lib\zipfile\__init__.py", line 1549, in getinfo
    raise KeyError(
KeyError: "There is no item named 'NULL' in the archive"

原因

将 docx 文件解压后,发现 DE862197-1772524393022.docx!\word\_rels\document.xml.rels 文件中的 Id="rId4"RelationshipTarget="../NULL"

解决办法

手动删除 docx 文件中的无效引用

  1. 打开文件 a.docx

  2. ctrl + f 打开左侧导航

  3. 点击搜索框右边的放大镜后,选择图形

  4. 使用红框右边的上下箭头逐个查看图片

  5. 删除或替换类似下图的图片

  6. 如下两个截图,有的图片很小,只显示一个红叉(下图 1),甚至有的图片就什么都不显示(下图 2),需要手动将图片尺寸放大后确认

  7. 必须使用"图形查找"逐个确认图片是否正常,否则会遗漏小图片或尺寸为 0 的图片

删除 Target="../NULL"Relationship 并重新打包为 docx

The image part with relationship rID8 was not found in Microsoft Word 中描述了大概的操作,但我试过不太好使,以下是我的操作:

  1. 先将文件 a.docx 重命名为 a.zip
  2. 将 a.zip 解药到 a 文件夹
  3. 使用记事本打开 a/word/_rels/document.xml.rels,查找 "null",并删除 <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../NULL"/>
  4. 重新将 a/ 文件夹下的所有文件压缩为 a-new.zip (不要包含 a 文件夹本身)
  5. 将 a-new.zip 重命名为 a-new.docx

解析 docx 时忽略 Target="../NULL"Relationship

此解决方案来自 Open Word docx file with "The image part with relationship rID8 was not found" error, it always fails · Issue #1105 · python-openxml/python-docx 的评论,替换 _SerializedRelationships.load_from_xml 方法的实现:

python 复制代码
import docx

from docx.opc.pkgreader import _SerializedRelationships, _SerializedRelationship
from docx.opc.oxml import parse_xml


def load_from_xml_v2(baseURI, rels_item_xml):
    """
    Return |_SerializedRelationships| instance loaded with the
    relationships contained in *rels_item_xml*. Returns an empty
    collection if *rels_item_xml* is |None|.
    """
    srels = _SerializedRelationships()
    if rels_item_xml is not None:
        rels_elm = parse_xml(rels_item_xml)
        for rel_elm in rels_elm.Relationship_lst:
            if rel_elm.target_ref in ('../NULL', 'NULL'):
                continue
            srels._srels.append(_SerializedRelationship(baseURI, rel_elm))
    return srels


_SerializedRelationships.load_from_xml = load_from_xml_v2

doc = docx.Document('a.docx')

下图中左边是原实现,右侧是新的实现 v2,添加了一个判断跳过了 Target="../NULL"Target="NULL"Relationship

参考

  1. KeyError: "There is no item named 'word/NULL' in the archive" · Issue #797 · python-openxml/python-docx
  2. fix: accommodate NULL relationship (by skipping) · Issue #678 · python-openxml/python-docx
  3. KeyError: "There is no item named 'word/NULL' in the archive" · Issue #797 · python-openxml/python-docx
  4. KeyError: "There is no item named 'word/NULL' in the archive" | YG 的零碎笔记
  5. The image part with relationship rID8 was not found in Microsoft Word - Microsoft 365 Apps | Microsoft Learn
相关推荐
Wenzar_1 小时前
**零信任架构下的微服务权限控制:用Go实现基于JWT的动态访问策略**在现代云原生环境中,
java·python·微服务·云原生·架构
不是起点的终点2 小时前
【实战】Python 一键生成数据库说明文档(对接阿里云百炼 AI,输出 Word 格式)
数据库·python·阿里云
2301_813599553 小时前
Go语言怎么做秒杀系统_Go语言秒杀系统实战教程【实用】
jvm·数据库·python
--fancy7 小时前
股票预测情感分析研究案例分析
python
shughui8 小时前
PyCharm 完整教程(旧版本卸载+旧/新版本下载安装+基础使用,2026最新版附安装包)
ide·python·pycharm
小糖学代码8 小时前
LLM系列:1.python入门:15.JSON 数据处理与操作
开发语言·python·json·aigc
yejqvow128 小时前
CSS如何控制placeholder文字的颜色_使用--placeholder伪元素
jvm·数据库·python
m0_743623929 小时前
HTML怎么创建多语言切换器_HTML语言选择下拉结构【指南】
jvm·数据库·python
pele9 小时前
Angular 表单中基于下拉选择动态启用字段必填校验的完整实现
jvm·数据库·python
HHHHH1010HHHHH9 小时前
Redis怎样判断节点是否主观下线_哨兵基于down-after-milliseconds参数的心跳超时判定
jvm·数据库·python