עבודה עם הערות

Aspose.Words מאפשר למשתמשים לעבוד עם הערות - הערות במסמך Aspose.Words מיוצגים על ידי Comment מעמד. השתמש גם CommentRangeStart ו CommentRangeEnd שיעורים כדי לציין את אזור הטקסט כי צריך להיות קשור עם תגובה.

הוסף תגובה

Aspose.Words מאפשר להוסיף הערות בכמה דרכים:

1.1 1. באמצעות Comment הכיתה 2. באמצעות CommentRangeStart ו CommentRangeEnd שיעורים

הדוגמה הבאה של הקוד מראה כיצד להוסיף תגובה לפסקה באמצעות הסעיף Comment קטגוריה:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.write("Some text is added.")
comment = aw.Comment(doc, "Awais Hafeez", "AH", datetime.today())
builder.current_paragraph.append_child(comment)
comment.paragraphs.add(aw.Paragraph(doc))
comment.first_paragraph.runs.add(aw.Run(doc, "Comment text."))
doc.save(docs_base.artifacts_dir + "WorkingWithComments.add_comments.docx")

הדוגמה הבאה של הקוד מראה כיצד להוסיף תגובה לפסקה באמצעות אזור טקסט והטקסט CommentRangeStart ו CommentRangeEnd שיעורים:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document()
para1 = aw.Paragraph(doc)
run1 = aw.Run(doc, "Some ")
run2 = aw.Run(doc, "text ")
para1.append_child(run1)
para1.append_child(run2)
doc.first_section.body.append_child(para1)
para2 = aw.Paragraph(doc)
run3 = aw.Run(doc, "is ")
run4 = aw.Run(doc, "added ")
para2.append_child(run3)
para2.append_child(run4)
doc.first_section.body.append_child(para2)
comment = aw.Comment(doc, "Awais Hafeez", "AH", datetime.today())
comment.paragraphs.add(aw.Paragraph(doc))
comment.first_paragraph.runs.add(aw.Run(doc, "Comment text."))
commentRangeStart = aw.CommentRangeStart(doc, comment.id)
commentRangeEnd = aw.CommentRangeEnd(doc, comment.id)
run1.parent_node.insert_after(commentRangeStart, run1)
run3.parent_node.insert_after(commentRangeEnd, run3)
commentRangeEnd.parent_node.insert_after(comment, commentRangeEnd)
doc.save(docs_base.artifacts_dir + "WorkingWithComments.anchor_comment.doc")

לחלץ או להסיר הערות

שימוש בהערות במסמך Word (בנוסף לשינויים במעקב) הוא תרגול נפוץ בעת בדיקת מסמכים, במיוחד כאשר ישנם מספר סוקרים. יש מצבים שבהם הדבר היחיד שאתה צריך במסמך הוא התגובות. נניח שאתה רוצה ליצור רשימה של ממצאי ביקורת, או אולי אספת את כל המידע השימושי של המסמך ואתה פשוט רוצה להסיר הערות מיותרות. ייתכן שתרצה להציג או להסיר הערות של מבקר מסוים.

בדגימה זו אנו הולכים לבחון כמה שיטות פשוטות עבור איסוף מידע מההערות בתוך מסמך ועל הסרת הערות ממסמכים. באופן ספציפי, אנו נעמוד כיצד:

  • הוציאו את כל התגובות של מסמך או רק את אלה שנעשו על ידי מחבר מסוים.
  • להסיר את כל ההערות של מסמך או רק ממחבר מסוים.

כיצד להסיר או להסיר הערות

הקוד במדגם זה הוא למעשה פשוט למדי וכל השיטות מבוססות על אותה גישה. תגובה במסמך Word מיוצגת על ידי Comment אובייקט Aspose.Words מסמך מודל אובייקט כדי לאסוף את כל התגובות במסמך להשתמש get_child_nodes שיטה עם הפרמטר הראשון להגדיר NodeType.COMMENT. ודא כי הפרמטר השני של Get_child_nodes השיטה מיועדת trueכוחות אלה Get_child_nodes כדי לבחור מכל בלוטות הילד, במקום רק לאסוף את הילדים המיידיים.

כדי להמחיש כיצד לחלץ ולהסיר הערות ממסמכים, אנו עוברים את השלבים הבאים:

1.1 1. פתח מסמך Word באמצעות Document הכיתה 1.1 1. קבלו את כל התגובות מהמסמכים לאוסף 1.1 1. להוציא הערות: 1 לעבור את האוסף באמצעות מפעיל ה-Foreach 1 לחלץ ולרשום את שם המחבר, תאריך וזמן וטקסט של כל הערות 1 לחלץ ולרשום את שם המחבר, תאריך וזמן וטקסט של הערות שנכתבו על ידי מחבר ספציפי, במקרה זה המחבר “ks” 1.1 1. להסיר הערות: 1 הפוך דרך האוסף באמצעות המפעיל

  1. Remove הערות 1.1 1. להציל את השינויים

כיצד להפיק את כל התגובות

The The The get_child_nodes שיטה היא מאוד שימושית ואתה יכול להשתמש בה בכל פעם שאתה צריך לקבל רשימה של דפי מסמך מכל סוג. האוסף המתקבל אינו יוצר כותרת מיידית, כי הצמתים נבחרים לתוך אוסף זה רק כאשר אתה אוכל או גישה פריטים בו.

דוגמה הקוד הבא מראה כיצד לחלץ את שם המחבר, תאריך וטקסט של כל התגובות במסמך:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def extract_comments(doc) :
collectedComments = []
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
for node in comments :
comment = node.as_comment()
collectedComments.append(comment.author + " " + comment.date_time.strftime("%Y-%m-%d %H:%M:%S") + " " + comment.to_string(aw.SaveFormat.TEXT))
return collectedComments

כיצד להפיק הערות של מחבר ספציפי

אחרי שבחרת Comment צומת לתוך אוסף, כל שעליך לעשות הוא לחלץ את המידע שאתה צריך. בדגימה זו, המחבר הראשוניים, התאריך, הזמן והטקסט הפשוט של ההערה משולבים לתוך מיתר אחד; אתה יכול לבחור לאחסן אותו בדרכים אחרות במקום.

השיטה המוגברת שמוציאה את ההערות של מחבר מסוים כמעט זהה, היא רק בודקת את שם המחבר לפני הוספת המידע למערך.

הדוגמה הבאה של הקוד מראה כיצד לחלץ את שם המחבר, תאריך וטקסט של ההערות של המחבר שצוין:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def extract_comments_by_author(doc, authorName) :
collectedComments = []
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
for node in comments :
comment = node.as_comment()
if (comment.author == authorName) :
collectedComments.append(comment.author + " " + comment.date_time.strftime("%Y-%m-%d %H:%M:%S") + " " + comment.to_string(aw.SaveFormat.TEXT))
return collectedComments

כיצד להסיר הערות

אם אתה מסיר את כל ההערות, אין צורך לעבור דרך הקולקציה המסלקת הערות אחד על ידי אחד; אתה יכול להסיר אותם על ידי קריאה clear על אוסף התגובות

הדוגמה הבאה של הקוד מראה כיצד להסיר את כל התגובות במסמך:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def remove_comments(doc) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
comments.clear()

כאשר אתה צריך להסיר הערות באופן סלקטיבי, התהליך הופך להיות דומה יותר לקוד שבו השתמשנו למיצוי תגובה.

הדוגמה הבאה של הקוד מראה כיצד להסיר הערות על ידי המחבר שצוין:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def remove_comments_by_author(doc, authorName) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
# Look through all comments and remove those written by the authorName.
for i in range(comments.count, 0) :
print(i)
comment = comments[i].as_comment()
if (comment.author == authorName) :
comment.remove()

הנקודה העיקרית להדגיש כאן היא השימוש של המפעיל. בניגוד למיצוי הפשוט, אתם רוצים למחוק תגובה. טריק מתאים הוא להפוך את האוסף לאחור מן האחרון Comment הראשון. הסיבה לכך אם אתה מתחיל מן הסוף והזיז לאחור, המדד של הפריטים הקודמים נשאר ללא שינוי, ואתה יכול לעבוד את דרכך חזרה אל הפריט הראשון באוסף.

הדוגמה הבאה של הקוד מציגה את השיטות למיצוי הערות וההסרה:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Comments.docx")
# Extract the information about the comments of all the authors.
for comment in self.extract_comments(doc) :
print(comment)
# Remove comments by the "pm" author.
self.remove_comments_by_author(doc, "pm")
print("Comments from \"pm\" are removed!")
# Extract the information about the comments of the "ks" author.
for comment in self.extract_comments_by_author(doc, "ks") :
print(comment)
# Read the comment's reply and resolve them.
self.comment_resolved_and_replies(doc)
# Remove all comments.
self.remove_comments(doc)
print("All comments are removed!")
doc.save(docs_base.artifacts_dir + "WorkingWithComments.process_comments.docx")

כיצד להסיר הערה בין CommentRangeStart ו- CommentRange End

שימוש Aspose.Words ניתן גם להסיר הערות בין CommentRangeStart ו CommentRangeEnd צומת

דוגמה לקוד הבא מראה כיצד להסיר טקסט בין CommentRangeStart ו CommentRangeEnd:

# Open the document.
doc = aw.Document(docs_base.my_dir + "Comments.docx")

commentStart = doc.get_child(aw.NodeType.COMMENT_RANGE_START, 0, True).as_comment_range_start()
commentEnd = doc.get_child(aw.NodeType.COMMENT_RANGE_END, 0, True).as_comment_range_end()

currentNode = commentStart
isRemoving = True
while (currentNode != None and isRemoving) :
    if (currentNode.node_type == aw.NodeType.COMMENT_RANGE_END) :
        isRemoving = False

    nextNode = currentNode.next_pre_order(doc)
    currentNode.remove()
    currentNode = nextNode

# Save the document.
doc.save(docs_base.artifacts_dir + "WorkingWithComments.remove_region_text.docx")

הוסף או להסיר תגובה

The The The add_reply השיטה מוסיפה תשובה להערה זו. שימו לב כי בשל הקיים Microsoft הגבלות Office רק רמה אחת של תשובות מותרות במסמך. יוצא דופן מסוג InvalidOperationException יעלה אם שיטה זו נקראת על תגובת התשובה הקיימת.

אתה יכול להשתמש remove_reply שיטה להסיר את התשובה המפורטת להערה זו.

הדוגמה הבאה של הקוד מראה כיצד להוסיף תשובה להגיב ולהסיר את התשובה:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
doc = aw.Document(docs_base.my_dir + "Comments.docx")
comment = doc.get_child(aw.NodeType.COMMENT, 0, True).as_comment()
comment.remove_reply(comment.replies[0])
comment.add_reply("John Doe", "JD", datetime(2017, 9, 25, 12, 15, 0), "New reply")
doc.save(docs_base.artifacts_dir + "WorkingWithComments.add_remove_comment_reply.docx")

תגובה

The The The replies רכוש מחזיר אוסף Comment פריטים שהם ילדים מיידיים של התגובה המפורטת.

הדוגמה הבאה של הקוד מראה כיצד לתקן באמצעות תשובות של תגובה ופתירתם:

# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET
@staticmethod
def comment_resolved_and_replies(doc) :
comments = doc.get_child_nodes(aw.NodeType.COMMENT, True)
parentComment = comments[0].as_comment()
for child in parentComment.replies :
childComment = child.as_comment()
# Get comment parent and status.
print(childComment.ancestor.id)
print(childComment.done)
# And update comment Done mark.
childComment.done = True