כתיבת קבצים DOCX עם LdmDocxWriter
כתיבת קבצים DOCX עם LdmDocxWriter
LdmDocxWriter להפוך A light_document_model.Document (LDM) אובייקט לתוך A קובץ DOCX. זהו הקובצין של כתיבה DocumentReader, אשר קורא את קבצי DOCX והם מופיעים כעץ LDM, יחד הם יוצרים צינור מסביב: DocumentReader בתוך , LdmDocxWriter יוצא החוצה.
דרישות
| דרישות | פרטים |
|---|---|
| Python | 3.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- הסופר מצפה ל- LDMDocument,לא ירוק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 |