การเขียนไฟล์ DOCX ด้วย LdmDocxWriter

การเขียนไฟล์ DOCX ด้วย LdmDocxWriter

การเขียนไฟล์ DOCX ด้วย LdmDocxWriter

LdmDocxWriter แปลง a light_document_model.Document (LDM) เป้าหมายใน DOCX file. มันคือตัวแทนเขียนของ DocumentReader, ที่อ่านไฟล์ DOCX และเปิดเผยมันเป็นต้นไม้ LDM. รวมกัน มันสร้างท่อการเดินทางกลับ: DocumentReader ใน, LdmDocxWriter ออกไปเลย.


Prerequisites

RequirementDetail
Python3.9 or later
Packageaspose-words-foss (มีใบอนุญาตจาก MIT)
InputA การทําการ 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")

เขียนไปยัง Bytes (In-Memory Output)

LdmDocxWriter.write_to_bytes(doc) กลับเนื้อหา DOCX เป็น bytes อัตถุประกอบ. ใช้นี่เมื่อคุณต้องการส่งไฟล์ผ่านเครือข่าย, เก็บมันไว้ในฐานข้อมูล หรือ ไม่ต้องเขียนไปยังระบบไฟล์ทั้งหมด.

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)


---

### รูปแบบการเดินทางกลับ

การใช้งานที่ทั่วไปที่สุดคือการอ่าน-ปรับเปลี่ยน-เขียน:

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")

การ เตือน ที่ เขียน ไป เสีย

When LdmDocxWriter ไม่สามารถแสดงให้เห็นการสร้าง LDM ในรูปแบบ DOCX ได้อย่างสมบูรณ์ มันปล่อยไฟฟ้าที่ DocxWriterLossyWarning.คุณสามารถจับภาพเหล่านี้ได้ด้วย Python’s 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)

เคล็ดลับและปฏิบัติที่ดีที่สุด

  • โทรมาตลอดเลย DocumentReader.to_light_document() ก่อนการส่งเอกสารไปยัง LdmDocxWriter ผู้เขียนคาดหวังว่า LDM Document, ไม่เป็นสกัด DocumentReader.
  • การใช้งาน write_to_bytes() ใน Web Handlers เพื่อหลีกเลี่ยงการจัดการไฟล์ชั่วคราว.
  • Check DocxWriterLossyWarning ในการทดสอบเพื่อตรวจพบการปรับปรุงกลับเมื่อ ปรับปรุงห้องสมุด.
  • LdmDocxWriter.options มีให้เลือกสําหรับการตั้งค่าระดับผู้เขียน; ตรวจสอบตัวเลขที่ object หลังจากการแสดงตัวอย่าง เพื่อดูที่ตั้งค่าที่มีอยู่.

ปัญหาทั่วไป

IssueCauseแก้ไข
FileNotFoundError ในวันที่ write()รายการแม่ไม่มีอยู่สร้างรายการผลิตด้วย Path.mkdir(parents=True, exist_ok=True) ก่อนโทรหา write()
DocxWriterLossyWarning ออกออกLDM สร้างไม่ได้แสดงใน DOCXลงความเตือน; ยอมรับการสูญเสียหรือปรับ LDM ก่อนเขียน
การออก DOCX ที่ว่างเปล่าเอกสาร LDM ไม่มีส่วนหรือข้อความVerify ldm_doc.sections และ ldm_doc.all_paragraphs ไม่ว่างหลังจากการบรรทุก

FAQ

LDM คืออะไร (light_document_model)?

LDM เป็นการแสดงแบบพีธอนของเอกสาร Word โดย Aspose.Words FOSS ซึ่งแยกล็อคเອກະສານจากรูปแบบ DOCX และทําให้ง่ายขึ้นในการตรวจสอบและปรับเปลี่ยนโครงสร้างเภทโดยโปรแกรม.

ผมเขียนได้เป็น PDF แทน DOCX ไหม?

ใช่ การใช้งาน LdmPdfWriter.write(doc, output_path) จากแพ็คเกจเดียวกันสําหรับ PDF ผลิตผล หรือ LdmMarkdownWriter.write(doc, output_path) สําหรับมาร์คดาวน์.

Does write_to_bytes() ต้องมีเส้นทางออก?

ไม่นะครับ. write_to_bytes(doc) ใช้เอกสาร LDM เท่านั้น และส่งกลับ a bytes อัตราสินค้า โดยไม่มีการปฏิสัมพันธ์ของระบบไฟล์.


คําอธิบายการพบบ่อย API

ประเภท / กลไกDescription
LdmDocxWriterเปลี่ยนแปลง LDM Document เป็นไฟล์ DOCX
LdmDocxWriter.write(doc, output_path)เขียน DOCX ไปยังเส้นทางไฟล์ที่ระบุ
LdmDocxWriter.write_to_bytes(doc)ส่งกลับ DOCX เนื้อหาเป็น bytes
LdmDocxWriter.optionsตัวเลือกการตั้งค่าเครื่องเขียน
DocxWriterLossyWarningการเตือนที่เกิดขึ้นเมื่อ LDM สร้างตกในระหว่างการเขียน
DocumentReaderอ่านไฟล์ DOCX และสร้างตัวแทน LDM
DocumentReader.to_light_document()ส่งเอกสารที่ถูกอัพโหลดเป็น LDM Document

See Also

 ภาษาไทย