Menulis File DOCX dengan LdmDocxWriter
Menulis File DOCX dengan LdmDocxWriter
LdmDocxWriter mengonversi objek light_document_model.Document (LDM) menjadi file DOCX. Ini merupakan padanan penulisan untuk DocumentReader, yang membaca file DOCX dan menampilkannya sebagai pohon LDM. Bersama-sama mereka membentuk pipeline putar balik: DocumentReader masuk, LdmDocxWriter keluar.
Prasyarat
| Persyaratan | Rincian |
|---|---|
| Python | 3.9 atau lebih baru |
| Package | aspose-words-foss (berlisensi MIT) |
| Input | Sebuah light_document_model.Document instance |
pip install aspose-words-fossMenulis ke Jalur File
LdmDocxWriter.write(doc, output_path) menyerialisasikan dokumen LDM ke file DOCX pada jalur yang diberikan. Jika direktori induk dari jalur tersebut tidak ada, pemanggilan akan memunculkan 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")Menulis ke Byte (Output Dalam Memori)
LdmDocxWriter.write_to_bytes(doc) mengembalikan konten DOCX sebagai objek bytes. Gunakan ini ketika Anda perlu mengirim file melalui jaringan, menyimpannya di basis data, atau menghindari penulisan ke sistem file sama sekali.
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)Pola Putar Balik
Kasus penggunaan paling umum adalah siklus baca-modifikasi-tulis:
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")Peringatan Penulisan Lossy
Ketika LdmDocxWriter tidak dapat merepresentasikan secara akurat konstruksi LDM dalam format DOCX, ia menghasilkan DocxWriterLossyWarning. Anda dapat menangkapnya dengan modul warnings milik Python untuk mengaudit apa yang dihilangkan:
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)Tips dan Praktik Terbaik
- Selalu panggil
DocumentReader.to_light_document()sebelum menyerahkan dokumen keLdmDocxWriter— penulis mengharapkanDocumentLDM, bukanDocumentReadermentah. - Gunakan
write_to_bytes()dalam penangan web untuk menghindari pengelolaan file sementara. - Periksa
DocxWriterLossyWarningdalam tes untuk mendeteksi regresi putar-balik saat memperbarui perpustakaan. LdmDocxWriter.optionstersedia untuk konfigurasi tingkat penulis; periksa objek setelah instansiasi untuk melihat pengaturan yang tersedia.
Masalah Umum
| Masalah | Penyebab | Perbaikan |
|---|---|---|
FileNotFoundError pada write() | Direktori induk tidak ada | Buat direktori output dengan Path.mkdir(parents=True, exist_ok=True) sebelum memanggil write() |
DocxWriterLossyWarning dikeluarkan | Konstruksi LDM tidak dapat direpresentasikan dalam DOCX | Tinjau pesan peringatan; terima kehilangan atau sesuaikan LDM sebelum menulis |
| DOCX output kosong | Dokumen LDM tidak memiliki bagian atau paragraf | Verifikasi ldm_doc.sections dan ldm_doc.all_paragraphs tidak kosong setelah dimuat |
FAQ
Apa itu LDM (light_document_model)?
LDM adalah representasi abstrak Aspose.Words FOSS, Python-native, dari dokumen Word. Ini memisahkan logika dokumen dari format biner DOCX dan mempermudah inspeksi serta manipulasi struktur dokumen secara programatik.
Apakah saya dapat menulis ke PDF alih-alih DOCX?
Ya — gunakan LdmPdfWriter.write(doc, output_path) dari paket yang sama untuk output PDF, atau LdmMarkdownWriter.write(doc, output_path) untuk Markdown.
Apakah write_to_bytes() memerlukan jalur output?
Tidak. write_to_bytes(doc) hanya mengambil dokumen LDM dan mengembalikan objek bytes tanpa interaksi sistem berkas.
Ringkasan API Reference
| Kelas / Metode | Deskripsi |
|---|---|
LdmDocxWriter | Mengonversi LDM Document menjadi file DOCX |
LdmDocxWriter.write(doc, output_path) | Menulis DOCX ke jalur file yang ditentukan |
LdmDocxWriter.write_to_bytes(doc) | Mengembalikan konten DOCX sebagai bytes |
LdmDocxWriter.options | Opsi konfigurasi penulis |
DocxWriterLossyWarning | Peringatan yang muncul ketika konstruksi LDM dibuang selama penulisan |
DocumentReader | Membaca file DOCX dan membangun representasi LDM |
DocumentReader.to_light_document() | Mengembalikan dokumen yang dimuat sebagai LDM Document |