כתיבת קבצים DOCX עם LdmDocxWriter

כתיבת קבצים DOCX עם LdmDocxWriter

כתיבת קבצים DOCX עם LdmDocxWriter

LdmDocxWriter להפוך A light_document_model.Document (LDM) אובייקט לתוך A קובץ DOCX. זהו הקובצין של כתיבה DocumentReader, אשר קורא את קבצי DOCX והם מופיעים כעץ LDM, יחד הם יוצרים צינור מסביב: DocumentReader בתוך , LdmDocxWriter יוצא החוצה.


דרישות

דרישותפרטים
Python3.9 or later
חבילהaspose-words-foss (הוא מורשה על ידי
כניסהא 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 כ- A 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.אתה יכול לתפוס את אלה עם 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() ב- Web managers להימנע ניהול קבצים זמני.
  • צפו DocxWriterLossyWarning במבחנים לזהות רגזים מסביב כאשר לשדרג את הספרייה.
  • LdmDocxWriter.options ניתן להגדיר את המערכת ברמה של סופר; לבדוק את לאחר ההגדרה, ניתן לראות את הגדרות הזמינות.

בעיות נפוצות

נושאסיבהקבע
FileNotFoundError על write()תיעוד הורים אינו קייםיצירת תיעוד היציאה עם Path.mkdir(parents=True, exist_ok=True) לפני שיקראו write()
DocxWriterLossyWarning הוצאתLDM Constructs לא מייצג ב-DOCXבדוק את ההודעה המזהירה; לקבל את הפסד או להתאים את ה- LDM לפני הכתיבה
תוצאות חיפוש DOCXמסמך ה- LDM אינו מכיל סעיפים או פרקים.בדוק ldm_doc.sections ו ldm_doc.all_paragraphs לא שקט לאחר העומס

FAQ

מה זה LDM (light_document_model)?

LDM הוא Aspose.Words FOSS, אסטרטגית, Python-native של מסמך Word. זה מחלק את ההיגיון של המסמכים מהפורמט הבינארי DOCX ומאפשר לך לבדוק ולנהל את מבנה המסכם בתכנית.

האם אני יכול לכתוב PDF במקום DOCX?

כן - שימוש LdmPdfWriter.write(doc, output_path) מתוך אותו חבילה עבור PDF תוצאות או LdmMarkdownWriter.write(doc, output_path) על מארק דון.

האם write_to_bytes() האם יש צורך בדרכים יוצאות?

לא. write_to_bytes(doc) לקחת רק את המסמך LDM ולהחזיר את 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

ראה גם

 עברית