كتابة ملفات DOCX باستخدام LdmDocxWriter

كتابة ملفات DOCX باستخدام LdmDocxWriter

كتابة ملفات DOCX باستخدام LdmDocxWriter

LdmDocxWriter تحويل a light_document_model.Document (LDM) كائن في ملف DOCX. هو النظير الكتابة لـ DocumentReader,، الذي يقرأ ملفات DOCX ويكشفهم كشجرة LDM. معاً يشكلون خط أنابيب ذهابا وإيابا: DocumentReader في،, LdmDocxWriter -إخرج .


المعايير

متطلباتالتفاصيل
Python3.9 or later
حزمةaspose-words-foss (مصدر من MIT)
المدخلات(أ) 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")

كتابة إلى بايت (مخرج داخل الذاكرة)

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 لا يمكن تمثيل بناء LDM بشكل صحيح في شكل DOCX إنها تصدر … DocxWriterLossyWarning.يمكنك التقاط هذه مع بايثون. 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() في معالج الويب لتجنب إدارة الملفات المؤقتة.
  • التحقق DocxWriterLossyWarning في الاختبارات للكشف عن التراجع ذهابًا وإيابًة عندما: تحديث المكتبة.
  • LdmDocxWriter.options متاح للتكوين على مستوى الكاتب؛ تحقق من بعد النسخة، لترى الإعدادات المتاحة.

القضايا المشتركة

القضيةالسببإصلاح
FileNotFoundError في write()دليل الأساسي غير موجودإنشاء دليل الخروج باستخدام: Path.mkdir(parents=True, exist_ok=True) قبل الاتصال write()
DocxWriterLossyWarning مُصدرةبناء LDM غير قابل للممثلة في DOCXاستعراض رسالة التحذير؛ قبول الخسارة أو ضبط LDM قبل الكتابة.
الصفحة الفارغة DOCXوثيقة LDM لا تحتوي على أقسام أو فقرات.التحقق من ذلك ldm_doc.sections و ldm_doc.all_paragraphs غير فارغة بعد التحميل.

FAQ

ما هو LDM (light_document_model)?

LDM هو تمثيل Word المستغرب في لغة Python من Aspose.Words FOSS . إنه ينفصل منطق المستند عن تنسيق DOCX الثنائي ويجعل التفتيش والتلاعب ببنية الوثائق بشكل برمجية أسهل.

هل يمكنني الكتابة إلى PDF بدلاً من DOCX؟?

نعم استخدام LdmPdfWriter.write(doc, output_path) من نفس الحزمة لـ PDF الناتج، أو LdmMarkdownWriter.write(doc, output_path) (مارك داون).

هل هو كذلك؟ write_to_bytes() تحتاج إلى مسار للخروج؟?

لا، أنا… write_to_bytes(doc) يأخذ فقط وثيقة LDM ويعيد a bytes المادة بدون تفاعل نظام الملفات.


إشارة API

الفئة / الطريقةوصف
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

انظر أيضا

 العربية