使用 DocumentReader 构建轻量文档模型

使用 DocumentReader 构建轻量文档模型

使用 DocumentReader 构建轻量文档模型

DocumentReader 是在 Aspose.Words FOSS 中读取 DOCX 文件的入口点。它接受文件路径、I/O 流或原始字节作为输入,然后将文档以 light_document_model.Document(LDM)的形式呈现——一个结构化的 Python 对象树,便于检查和转换。


先决条件

需求详细信息
Python3.9 或更高
Packageaspose-words-foss(MIT 许可证)
Input一个 .docx 文件、流或 bytes
pip install aspose-words-foss

从文件路径加载

DocumentReader.load_file(filepath) 通过路径读取 DOCX 文件。随后调用 to_light_document() 以获取 LDM 表示。

from aspose.words_foss import DocumentReader

reader = DocumentReader()
reader.load_file("data/report.docx")
doc = reader.to_light_document()

print("Page count:", doc.page_count)
print("Sections:", len(doc.sections))

从流加载

DocumentReader.load_stream(stream) 接受任何具有 read() 方法的类文件对象。这对于来自 HTTP 响应、ZIP 存档或内存缓冲区的数据非常有用。

from aspose.words_foss import DocumentReader

reader = DocumentReader()
with open("data/report.docx", "rb") as fh:
    reader.load_stream(fh)

doc = reader.to_light_document()
print("Paragraphs:", len(doc.all_paragraphs))

从字节加载

DocumentReader.load_bytes(data) 接受一个 bytes 或 bytearray 对象。当 DOCX 内容已经在内存中时使用(例如从数据库获取或以编程方式构建)。

from aspose.words_foss import DocumentReader

with open("data/report.docx", "rb") as fh:
    data = fh.read()

reader = DocumentReader()
reader.load_bytes(data)
doc = reader.to_light_document()
print("Text preview:", doc.text[:200])

导航 LDM 结构

调用 to_light_document() 后,返回的 Document 对象会展示完整的文档树:

from aspose.words_foss import DocumentReader

reader = DocumentReader()
reader.load_file("data/report.docx")
doc = reader.to_light_document()

# Sections
for section in doc.sections:
    print("Section paragraphs:", len(section.paragraphs))

# All paragraphs (flat list across all sections)
for para in doc.all_paragraphs:
    if para.text.strip():
        print(" -", para.text[:80])

# Headings
for heading in doc.headings(max_level=2):
    print("H:", heading.text)

读取完整文档文本

Document.text 返回文档的完整纯文本内容,拼接所有章节中的段落文本:

reader = DocumentReader()
reader.load_file("data/report.docx")
doc = reader.to_light_document()
print(doc.text[:500])

提示与最佳实践

  • 在调用 to_light_document() 之前,总是先调用 load_file()、load_stream() 或 load_bytes() —— 在未加载的读取器上调用 to_light_document() 可能会引发错误或返回空文档。
  • 当你在内存中控制文档生命周期时,请使用 load_bytes();它可以避免打开文件句柄。
  • 使用 doc.all_paragraphs 对所有段落进行平铺迭代;当需要逐节处理时,使用 doc.sections。
  • DocumentReader 实现了 LdmBuilderMixin,这意味着 to_light_document() 作为混入方法可用 —— 同一接口在库中的其他读取器类之间共享。

常见问题

问题原因修复
加载后 doc.sections 为空文件不是有效的 DOCX请确认文件能在 Word 中打开;检查它未被密码保护
doc.all_paragraphs 为空文档没有正文(例如仅有页眉/页脚)请改为检查 doc.header_paragraphs 和 doc.footer_paragraphs
AttributeError 在 to_light_document() 上在转换之前未调用加载方法首先调用 load_file()、load_stream() 或 load_bytes() 中的一个

FAQ

什么是 LDM?

轻文档模型(light_document_model.Document)是一种抽象的、基于 Python 本地的对象树,用于表示 Word 文档。它将文档逻辑与 DOCX 二进制格式分离,并旨在实现简便的程序化操作。

我可以修改 LDM 并将其写回 DOCX 吗?

是的。修改后 doc 对象,将其传递给 LdmDocxWriter.write(doc, path) 以生成更新后的 DOCX 文件。参见 LdmDocxWriter 指南 了解详情。

LdmBuilderMixin 添加了什么?

LdmBuilderMixin 提供了 to_light_document() 方法。DocumentReader 继承自它,因此您始终通过 reader 实例访问此方法。


API Reference 摘要

类 / 方法描述
DocumentReader读取 DOCX 输入并构建 LDM 表示
DocumentReader.load_file(filepath)通过路径加载 DOCX 文件
DocumentReader.load_stream(stream)从类似文件的流加载
DocumentReader.load_bytes(data)从 bytes 对象加载
DocumentReader.to_light_document()返回已加载的文档为 LDM Document
LdmBuilderMixin提供 to_light_document() 的 Mixin
Document.sections文档中的章节列表
Document.all_paragraphs所有段落的扁平列表
Document.text文档的完整纯文本内容
Document.page_count页数
Document.headings(max_level)截至 max_level 的标题段落列表

另请参阅

 中文