使用 LdmDocxWriter 编写 DOCX 文件
使用 LdmDocxWriter 编写 DOCX 文件
LdmDocxWriter 将 light_document_model.Document(LDM)对象转换为 DOCX 文件。它是 DocumentReader 的写入对应组件,后者读取 DOCX 文件并将其展现为 LDM 树。两者共同构成往返管道:DocumentReader 输入,LdmDocxWriter 输出。
先决条件
| 需求 | 细节 |
|---|---|
| Python | 3.9 或更高版本 |
| Package | aspose-words-foss (MIT-licensed) |
| Input | 一个 light_document_model.Document 实例 |
pip install aspose-words-foss写入文件路径
LdmDocxWriter.write(doc, output_path) 将 LDM 文档序列化为指定路径下的 DOCX 文件。如果路径的父目录不存在,调用将抛出 FileNotFoundError。
from aspose.words_foss import DocumentReader, LdmDocxWriter
# Load a DOCX and obtain its LDM representation
reader = DocumentReader()
reader.load_file("input/original.docx")
ldm_doc = reader.to_light_document()
# Write the LDM document back to a DOCX file
writer = LdmDocxWriter()
writer.write(ldm_doc, "output/result.docx")
print("Written to output/result.docx")写入字节(内存输出)
LdmDocxWriter.write_to_bytes(doc) 将 DOCX 内容作为 bytes 对象返回。当您需要通过网络发送文件、存储到数据库或完全避免写入文件系统时使用它。
from aspose.words_foss import DocumentReader, LdmDocxWriter
reader = DocumentReader()
reader.load_file("input/report.docx")
ldm_doc = reader.to_light_document()
writer = LdmDocxWriter()
docx_bytes = writer.write_to_bytes(ldm_doc)
print(f"DOCX bytes: {len(docx_bytes)}")
# Save manually if desired
with open("output/report_copy.docx", "wb") as f:
f.write(docx_bytes)往返模式
最常见的用例是读-改-写往返:
from aspose.words_foss import DocumentReader, LdmDocxWriter
reader = DocumentReader()
reader.load_file("input/template.docx")
ldm_doc = reader.to_light_document()
# Inspect or modify the LDM here
print("Sections:", len(ldm_doc.sections))
print("Paragraphs:", len(ldm_doc.all_paragraphs))
writer = LdmDocxWriter()
writer.write(ldm_doc, "output/modified.docx")有损写入警告
当 LdmDocxWriter 无法在 DOCX 格式中忠实地表示 LDM 构造时,它会发出一个 DocxWriterLossyWarning。您可以使用 Python 的 warnings 模块捕获这些信息,以审计被丢弃的内容:
import warnings
from aspose.words_foss import DocumentReader, LdmDocxWriter, DocxWriterLossyWarning
reader = DocumentReader()
reader.load_file("input/complex.docx")
ldm_doc = reader.to_light_document()
writer = LdmDocxWriter()
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", DocxWriterLossyWarning)
writer.write(ldm_doc, "output/complex_out.docx")
for warning in w:
print("Lossy:", warning.message)技巧与最佳实践
- 在将文档传递给
LdmDocxWriter之前,请始终调用DocumentReader.to_light_document()—— 写入器期望的是 LDMDocument,而不是原始的DocumentReader。 - 在 Web 处理程序中使用
write_to_bytes()以避免临时文件管理。 - 在测试中检查
DocxWriterLossyWarning以检测升级库时的往返回归。 LdmDocxWriter.options可用于写入器级别的配置;实例化后检查对象以查看可用设置。
常见问题
| 问题 | 原因 | 修复 |
|---|---|---|
FileNotFoundError 在 write() | 父目录不存在 | 在调用 write() 之前,使用 Path.mkdir(parents=True, exist_ok=True) 创建输出目录 |
DocxWriterLossyWarning 已发出 | LDM 构造无法在 DOCX 中表示 | 检查警告信息;接受丢失或在写入前调整 LDM |
| 空的输出 DOCX | LDM 文档没有章节或段落 | 验证 ldm_doc.sections 和 ldm_doc.all_paragraphs 在加载后非空 |
FAQ
什么是 LDM (light_document_model)?
LDM 是 Aspose.Words FOSS 的抽象的、Python 本地表示形式,用于 Word 文档。它将文档逻辑与 DOCX 二进制格式解耦,并使以编程方式检查和操作文档结构更容易。
我可以写入 PDF 而不是 DOCX 吗?
可以 — 使用同一软件包中的 LdmPdfWriter.write(doc, output_path) 进行 PDF 输出,或使用 LdmMarkdownWriter.write(doc, output_path) 输出为 Markdown。
write_to_bytes() 需要输出路径吗?
不。write_to_bytes(doc) 只接受 LDM 文档,并返回一个 bytes 对象,不进行文件系统交互。
API Reference 摘要
| 类 / 方法 | 描述 |
|---|---|
LdmDocxWriter | 将 LDM Document 转换为 DOCX 文件 |
LdmDocxWriter.write(doc, output_path) | 将 DOCX 写入指定的文件路径 |
LdmDocxWriter.write_to_bytes(doc) | 以 bytes 形式返回 DOCX 内容 |
LdmDocxWriter.options | 写入器配置选项 |
DocxWriterLossyWarning | 在写入过程中丢弃 LDM 构造时触发的警告 |
DocumentReader | 读取 DOCX 文件并构建 LDM 表示 |
DocumentReader.to_light_document() | 返回加载的文档为 LDM Document |