Tạo file DOCX với LdmDocxWriter

Tạo file DOCX với LdmDocxWriter

Tạo file DOCX với LdmDocxWriter

LdmDocxWriter chuyển đổi a light_document_model.Document (LDM) đối tượng vào một file DOCX. nó là chữ viết đối tác để DocumentReader, đọc các tập tin DOCX và tiếp xúc chúng như một cây LDM. Cùng nhau họ hình thành một đường ống vòng tròn: DocumentReader Trong, LdmDocxWriter ra ngoài.


Prerequisites

RequirementDetail
Python3.9 or later
Packageaspose-words-foss (Một giấy phép)
InputA light_document_model.Document Thông tin
pip install aspose-words-foss

Viết đến một file path

LdmDocxWriter.write(doc, output_path) Xét bản tài liệu LDM sang DOCX file tại con đường cụ thể. nếu thư mục mẹ của con Đường không có cuộc gọi sẽ tăng lên 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")

Sách viết cho các bites (In-Memory Output)

LdmDocxWriter.write_to_bytes(doc) - Khởi động nội dung DOCX như một bytes đối tượng. Sử dụng nó khi bạn cần gửi tệp qua mạng, lưu trữ nó trong cơ sở dữ liệu hoặc Tránh viết vào hệ thống tệp hoàn toàn.

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)


---

### Mẫu vòng tròn

Trường hợp sử dụng phổ biến nhất là một chuyến đi vòng đọc-thay đổi-tự viết:

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

Lời cảnh báo Lossy-Write

When LdmDocxWriter Không thể trung thành đại diện cho một LDM xây dựng trong định dạng DOCX Nó phát hành a DocxWriterLossyWarning.Bạn có thể chụp chúng với Python. warnings Module để kiểm tra những gì đã bị rơi:

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)

Mẹo và Thực hành Tốt nhất

  • Luôn gọi DocumentReader.to_light_document() Trước khi thông qua tài liệu cho LdmDocxWriter - Nhà văn mong đợi một LDM Document,Không phải là raw DocumentReader.
  • Sử dụng write_to_bytes() trong các nhà quản lý web để tránh việc quản trị tệp tạm thời.
  • Check DocxWriterLossyWarning trong các thử nghiệm để phát hiện trở lại vòng tròn khi nâng cấp thư viện.
  • LdmDocxWriter.options có sẵn cho cấu hình cấp bằng văn bản; kiểm tra các object sau khi instantiation để xem các cài đặt có sẵn.

Các vấn đề thường gặp

IssueCauseFix Đặt
FileNotFoundError trên write()Thư viện cha mẹ không tồn tạiTạo thư mục xuất khẩu với Path.mkdir(parents=True, exist_ok=True) Trước khi gọi write()
DocxWriterLossyWarning phát hànhLDM Constructs không thể đại diện trong DOCXXem tin nhắn cảnh báo; chấp nhận mất hoặc điều chỉnh LDM trước khi viết
Tải về DOCXTài liệu LDM không có phần hoặc đoạnVerify ldm_doc.sectionsldm_doc.all_paragraphs Không được làm trống sau khi tải

FAQ

Mã sản phẩm là gì (light_document_model)?

LDM là Aspose.Words FOSS, đại diện từ Python của một tài liệu Word. Nó phân chia logic tài khoản khỏi định dạng nhị phân DOCX và làm cho nó dễ dàng hơn để kiểm tra và thao túng cấu trúc tài chính theo lập trình.

Tôi có thể viết PDF thay vì DOCX không?

Có - Sử dụng LdmPdfWriter.write(doc, output_path) từ cùng một gói cho PDF sản xuất, hoặc LdmMarkdownWriter.write(doc, output_path) cho Markdown.

Does write_to_bytes() Cần có một con đường xuất phát?

Không . write_to_bytes(doc) Chỉ lấy tài liệu LDM và trả lại một bytes đối tượng Không có hệ thống file tương tác.


Tóm tắt Tham chiếu API

Class / Phương phápDescription
LdmDocxWriterChuyển đổi LDM Document - Tải file DOCX
LdmDocxWriter.write(doc, output_path)Viết DOCX vào con đường tệp được chỉ định
LdmDocxWriter.write_to_bytes(doc)Khởi động lại nội dung DOCX như: bytes
LdmDocxWriter.optionsTùy chọn cấu hình Writer
DocxWriterLossyWarningCảnh báo được nâng lên khi một cấu trúc LDM bị rơi trong quá trình viết
DocumentReaderĐọc một tệp DOCX và xây dựng đại diện LDM
DocumentReader.to_light_document()Trả lại tài liệu tải về như một LDM Document

See Also

 Tiếng Việt