הכנס ונספח מסמכים
לפעמים צריך לשלב כמה מסמכים לאחד. אתה יכול לעשות את זה באופן ידני או שאתה יכול להשתמש Aspose.Words להוסיף או להוסיף תכונה.
ניתוח הכנס מאפשר לך להוסיף את התוכן של מסמכים שנוצרו בעבר לתוך חדש או קיים.
בתורו, התכונה Append מאפשר לך להוסיף מסמך רק בסוף מסמך אחר.
מאמר זה מסביר כיצד להוסיף או לאמת מסמך זה בדרכים שונות, ומתאר את התכונות הנפוצות שאתה יכול ליישם בעת הוספת מסמכים או יישום.
הכנס מסמך
כאמור לעיל, Aspose.Words מסמך מיוצג כעץ של צמתים, ופעולת הוספת מסמך אחד לשני היא העתקת צמתים מן עץ המסמך הראשון אל השני.
ניתן להוסיף מסמכים במגוון מיקומים בדרכים שונות. לדוגמה, באפשרותך להוסיף מסמך באמצעות הפעלה חלופית, שדה מיזוג במהלך פעולת מיזוג, או באמצעות סימן ספר.
אתה יכול גם להשתמש insert_document או insert_document_inline שיטה, אשר דומה להכניס מסמך Microsoft Word, כדי להוסיף מסמך שלם בעמדה הנוכחית של cursor ללא ייבוא קודם.
הדוגמה הבאה של הקוד מראה כיצד להוסיף מסמך באמצעות המונחים: document שיטה:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
src_doc = aw.Document(MY_DIR + "Document source.docx") | |
dst_doc = aw.Document(MY_DIR + "Northwind traders.docx") | |
builder = aw.DocumentBuilder(dst_doc) | |
builder.move_to_document_end() | |
builder.insert_break(aw.BreakType.PAGE_BREAK) | |
builder.insert_document(src_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) | |
builder.document.save(ARTIFACTS_DIR + "JoinAndAppendDocuments.insert_document.docx") |
הדוגמה הבאה של הקוד מראה כיצד להוסיף מסמך באמצעות המונחים:_document_inline שיטה:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
src_doc = aw.DocumentBuilder() | |
src_doc.write("[src content]") | |
# Create destination document. | |
dst_doc = aw.DocumentBuilder() | |
dst_doc.write("Before ") | |
dst_doc.insert_node(aw.BookmarkStart(dst_doc.document, "src_place")) | |
dst_doc.insert_node(aw.BookmarkEnd(dst_doc.document, "src_place")) | |
dst_doc.write(" after") | |
self.assertEqual("Before after", dst_doc.document.get_text().strip()) | |
# Insert source document into destination inline. | |
dst_doc.move_to_bookmark("src_place") | |
dst_doc.insert_document_inline(src_doc.document, aw.ImportFormatMode.USE_DESTINATION_STYLES, aw.ImportFormatOptions()) | |
self.assertEqual("Before [src content] after", dst_doc.document.get_text().strip()) |
הקטעים הבאים מתארים את האפשרויות שבהן ניתן להכניס מסמך אחד לשני.
הכנס מסמך ב- Bookmark
אתה יכול לייבא קובץ טקסט לתוך מסמך ולהכניס אותו מיד לאחר סימן ספר שהגדרת במסמך. כדי לעשות זאת, ליצור סעיף מסומנים שבו אתה רוצה את המסמך להיות מוכנס.
הדוגמה הבאה מציגה כיצד להכניס את התוכן של מסמך אחד לסימן ספר במסמך אחר:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
main_doc = aw.Document(MY_DIR + "Document insertion 1.docx") | |
sub_doc = aw.Document(MY_DIR + "Document insertion 2.docx") | |
bookmark = main_doc.range.bookmarks.get_by_name("insertionPlace") | |
self.insert_document(bookmark.bookmark_start.parent_node, sub_doc) | |
main_doc.save(ARTIFACTS_DIR + "CloneAndCombineDocuments.insert_document_at_bookmark.docx") |
אישור מסמך
ייתכן שיש לך מקרה שימוש שבו אתה צריך לכלול דפים נוספים ממסמכים עד סוף מסמך קיים. כדי לעשות זאת, אתה רק צריך לקרוא append_document שיטה להוסיף מסמך עד סוף אחד.
דוגמה לקוד הבא מראה כיצד להגיש מסמך לסיום מסמך אחר:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
dst_doc = aw.Document() | |
dst_doc.first_section.body.append_paragraph("Destination document text. ") | |
src_doc = aw.Document() | |
src_doc.first_section.body.append_paragraph("Source document text. ") | |
# Append the source document to the destination document. | |
# Pass format mode to retain the original formatting of the source document when importing it. | |
dst_doc.append_document(src_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) | |
dst_doc.save(ARTIFACTS_DIR + "JoinAndAppendDocuments.keep_source_formatting.docx") |
יבוא והכנסת Nodes באופן ידני
Aspose.Words מאפשר לך להוסיף ולערוך מסמכים באופן אוטומטי ללא דרישות ייבוא קודמות. עם זאת, אם אתה צריך להוסיף או לאשר צומת מסוים של המסמך שלך, כגון סעיף או פסקה, ולאחר מכן קודם אתה צריך לייבא את הצומת הזה באופן ידני.
כאשר אתה צריך להוסיף או לאשר סעיף אחד או פסקה אחד לאחר, אתה בעצם צריך לייבא את הצמתים של עץ הצומת הראשון לתוך השני באמצעות השני באמצעות שימוש. import_node שיטה. לאחר ייבוא הצמתים שלך, עליך להשתמש insert_after / / / / insert_before שיטה להכניס צומת חדש לאחר / לפני צומת ההתייחסות. זה מאפשר לך להתאים אישית את תהליך ההחדרה על ידי ייבוא צמתים מ מסמך והוספתו במיקומים מסוימים.
אתה יכול גם להשתמש append_child שיטה להוסיף צומת חדש שצוין עד סוף הרשימה של בלוטות הילד, למשל, אם ברצונך להוסיף תוכן ברמת הסעיף במקום ברמת הסעיף.
הדוגמה הבאה של הקוד מראה כיצד לייבא באופן ידני צומתים ולהכניס אותם לאחר צומת מסוים באמצעות צומת ספציפי. insert_after שיטה:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
@staticmethod | |
def insert_document(insertion_destination: aw.Node, doc_to_insert: aw.Document): | |
"""Inserts content of the external document after the specified node. | |
Section breaks and section formatting of the inserted document are ignored. | |
:param insertion_destination: Node in the destination document after which the content | |
Should be inserted. This node should be a block level node (paragraph or table). | |
:param doc_to_insert: The document to insert. | |
""" | |
if insertion_destination.node_type not in (aw.NodeType.PARAGRAPH, aw.NodeType.TABLE): | |
raise ValueError("The destination node should be either a paragraph or table.") | |
destination_parent = insertion_destination.parent_node | |
importer = aw.NodeImporter(doc_to_insert, insertion_destination.document, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) | |
# Loop through all block-level nodes in the section's body, | |
# then clone and insert every node that is not the last empty paragraph of a section. | |
for src_section in doc_to_insert.sections: | |
for src_node in src_section.as_section().body.get_child_nodes(aw.NodeType.ANY, False): | |
if src_node.node_type == aw.NodeType.PARAGRAPH: | |
para = src_node.as_paragraph() | |
if para.is_end_of_section and not para.has_child_nodes: | |
continue | |
new_node = importer.import_node(src_node, True) | |
destination_parent.insert_after(new_node, insertion_destination) | |
insertion_destination = new_node |
התוכן מיובא לתוך סעיף מסמך היעד לפי סעיף, כלומר הגדרות, כגון הגדרות דף, כותרות או רגלים, נשמרות במהלך היבוא. זה גם שימושי לציין כי אתה יכול להגדיר הגדרות פורמט בעת הוספת או נספח מסמך כדי לציין כיצד שני מסמכים משולבים יחד.
תכונות נפוצות עבור הכנס ונספח מסמכים
שניהם insert_document ו append_document שיטות לקבל ImportFormatMode ו ImportFormatOptions כפרמטרי קלט. The The The ImportFormatMode מאפשר לך לשלוט כיצד פורמט מסמך ממוזג כאשר אתה לייבא תוכן מ מסמך אחד למשנהו על ידי בחירת מצבי פורמט שונים כגון USE_DESTINATION_STYLES, KEEP_SOURCE_FORMATTING, ו KEEP_DIFFERENT_STYLES. The The The ImportFormatOptions מאפשר לך לבחור אפשרויות ייבוא שונות כגון ignore_header_footer, ignore_text_boxes, keep_source_numbering, merge_pasted_lists, ו smart_style_behavior.
Aspose.Words מאפשר לך להתאים את ויזואליזציה של מסמך וכתוצאה מכך כאשר שני מסמכים מתווספים יחד בפעולת כניסה או אפליקציה על ידי שימוש Section ו PageSetup. The The The page_setup רכוש מכיל את כל התכונות של סעיף כגון section_start restart_page_numbering, page_starting_number, orientation, ואחרים. מקרה השימוש הנפוץ ביותר הוא להגדיר את section_start רכוש כדי להגדיר אם התוכן הנוסף יופיע באותו דף או פיצול לתוך חדש.
הדוגמה הבאה של הקוד מראה כיצד לתקן מסמך אחד לאחר תוך שמירה על התוכן מפיצול בשני דפים:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET.git. | |
src_doc = aw.Document(MY_DIR + "Document source.docx") | |
dst_doc = aw.Document(MY_DIR + "Northwind traders.docx") | |
# Set the source document to continue straight after the end of the destination document. | |
src_doc.first_section.page_setup.section_start = aw.SectionStart.CONTINUOUS | |
# Restart the page numbering on the start of the source document. | |
src_doc.first_section.page_setup.restart_page_numbering = True | |
src_doc.first_section.page_setup.page_starting_number = 1 | |
# To ensure this does not happen when the source document has different page setup settings, make sure the | |
# settings are identical between the last section of the destination document. | |
# If there are further continuous sections that follow on in the source document, | |
# this will need to be repeated for those sections. | |
src_doc.first_section.page_setup.page_width = dst_doc.last_section.page_setup.page_width | |
src_doc.first_section.page_setup.page_height = dst_doc.last_section.page_setup.page_height | |
src_doc.first_section.page_setup.orientation = dst_doc.last_section.page_setup.orientation | |
# Iterate through all sections in the source document. | |
for para in src_doc.get_child_nodes(aw.NodeType.PARAGRAPH, True): | |
para = para.as_paragraph() | |
para.paragraph_format.keep_with_next = True | |
dst_doc.append_document(src_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING) | |
dst_doc.save(ARTIFACTS_DIR + "JoinAndAppendDocuments.different_page_setup.docx") |