LdmDocxWriterでDOCXファイルを書き込む

LdmDocxWriterでDOCXファイルを書き込む

LdmDocxWriter を使用した DOCX ファイルの書き込み

LdmDocxWriter は light_document_model.Document (LDM) オブジェクトを DOCX ファイルに変換します。これは DOCX ファイルを読み取り、LDM ツリーとして公開する DocumentReader の書き込み対応部分です。これらを合わせると往復パイプラインが構成されます: DocumentReader が入力、LdmDocxWriter が出力です。


前提条件

要件詳細
Python3.9 以降
Packageaspose-words-foss (MIT ライセンス)
Inputlight_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() を呼び出してください — ライターは生の DocumentReader ではなく、LDM Document を期待しています。
  • Web ハンドラ内で write_to_bytes() を使用し、一時ファイルの管理を回避してください。
  • ライブラリをアップグレードする際に、テストで DocxWriterLossyWarning をチェックしてラウンドトリップの回帰を検出してください。
  • LdmDocxWriter.options はライターレベルの設定に利用できます。インスタンス化後にオブジェクトを調べて、利用可能な設定を確認してください。

一般的な問題

問題原因修正
FileNotFoundError(write()上)親ディレクトリが存在しませんwrite() を呼び出す前に、Path.mkdir(parents=True, exist_ok=True) を使用して出力ディレクトリを作成してください
DocxWriterLossyWarning が出力されましたDOCX では LDM 構造を表現できません警告メッセージを確認し、損失を受け入れるか、書き込む前に LDM を調整してください
出力 DOCX が空ですLDM ドキュメントにセクションや段落がありませんロード後に ldm_doc.sections と ldm_doc.all_paragraphs が空でないことを確認してください

FAQ

LDM(light_document_model)とは何ですか?

LDM は Aspose.Words FOSS の抽象化された、Python ネイティブの Word 文書表現です。DOCX バイナリ形式から文書ロジックを切り離し、プログラムから文書構造を検査・操作しやすくします。

DOCX の代わりに PDF に書き出すことはできますか?

はい — 同じパッケージの LdmPdfWriter.write(doc, output_path) を使用して PDF 出力を行うか、Markdown には LdmMarkdownWriter.write(doc, output_path) を使用してください。

write_to_bytes()は出力パスが必要ですか?

いいえ。write_to_bytes(doc)はLDMドキュメントのみを受け取り、ファイルシステムとのやり取りなしでbytesオブジェクトを返します。


API Reference概要

クラス / メソッド説明
LdmDocxWriterLDM Document を DOCX ファイルに変換します
LdmDocxWriter.write(doc, output_path)DOCX を指定されたファイルパスに書き込みます
LdmDocxWriter.write_to_bytes(doc)DOCX の内容を bytes として返します
LdmDocxWriter.optionsWriter の構成オプション
DocxWriterLossyWarning書き込み中に LDM 構成要素が除外されたときに発生する警告
DocumentReaderDOCX ファイルを読み込み、LDM 表現を構築します
DocumentReader.to_light_document()ロードされたドキュメントを LDM Document として返します

参照

 日本語