دانلود فایل های DOCX با LdmDocxWriter

دانلود فایل های DOCX با LdmDocxWriter

دانلود فایل های DOCX با LdmDocxWriter

LdmDocxWriter تبدیل A light_document_model.Document (LDM) اشیاء در یک فایل DOCX: این همان نویسنده است که به عنوان DocumentReader, که فایل های DOCX را می خواند و آنها را به عنوان یک درخت LDM قرار می دهد.با هم آنها یک خط لوله گرد شکل می دهند: DocumentReader در آن،, LdmDocxWriter بیرون می آید.


Prerequisites

RequirementDetail
Python3.9 or later
Packageaspose-words-foss (مطالعه مجوز)
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")

نوشتن به بیت ها (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 ضبط کنید. 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() در وب سرورها برای جلوگیری از مدیریت موقت فایل ها.
  • Check DocxWriterLossyWarning در آزمایشات برای تشخیص بازگشت های چرخشی هنگامی که به روز رسانی کتابخانه.
  • LdmDocxWriter.options برای تنظیمات سطح نویسنده در دسترس است؛ بررسی بعد از این که به صورت مستقیم تنظیمات موجود را مشاهده کردید.

مسائل مشترک

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 Aspose.Words FOSS است، نمایندگی پایتون-مأمور یک سند Word. آن را از منطق اسناد از فرمت دوگانه DOCX و آسان تر برای بررسی و دستکاری ساختار اسنادی به طور برنامه نویسی.

آیا می توانم به جای DOCX به PDF بنویسم؟?

بله - استفاده LdmPdfWriter.write(doc, output_path) از همان بسته برای PDF تولید یا LdmMarkdownWriter.write(doc, output_path) برای مارک دایناسور.

Does write_to_bytes() نیاز به یک مسیر خروجی دارد؟?

نه . write_to_bytes(doc) فقط یک سند LDM را دریافت می کند و به آن بازگردانده می شود. 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

 فارسی