使用Python在Word文檔中添加,刪除和回復(fù)批注
在文檔協(xié)作與審閱場景中,高效管理批注是提升團(tuán)隊效率的關(guān)鍵環(huán)節(jié)。通過編程手段自動化批注操作,不僅能避免人工重復(fù)勞動帶來的誤差,還可實現(xiàn)跨版本批注追蹤、批量處理歷史反饋等復(fù)雜需求。例如,自動添加批注能將標(biāo)準(zhǔn)化檢查結(jié)果嵌入文檔,刪除冗余批注可保持文檔整潔性,回復(fù)批注可構(gòu)建完整的審閱對話鏈,對軟件開發(fā)文檔審核、學(xué)術(shù)論文修訂等需要嚴(yán)格版本控制的場景具有顯著實用價值。本文將介紹如何使用Python在Word文檔中添加、刪除和回復(fù)批注,提供步驟介紹和代碼示例。
本文使用的方法需要用到免費(fèi)的Free Spire.Doc for Python,PyPI:pip install spire.doc。
用Python在Word文檔中添加批注
我們可以使用Paragraph.AppendComment()方法在Word文檔中添加批注,然后使用Comment.Format.Author屬性對批注的作者進(jìn)行設(shè)置,并設(shè)置好批注的開始和結(jié)束標(biāo)記,即可完成批注的創(chuàng)建。以下是操作步驟:
- 導(dǎo)入所需模塊。
- 創(chuàng)建Document對象,使用Document.LoadFromFile()方法載入Word文檔。
- 使用Document.Sections.get_Item()方法獲取指定節(jié)。
- 使用Section.Paragraphs.get_Item()方法獲取指定段落。
- 使用Paragraph.AppendComment()方法添加批注到段落。
- 使用Comment.Format.Author屬性設(shè)置批注作者。
- 通過創(chuàng)建CommentMark對象,創(chuàng)建批注開始和結(jié)束標(biāo)記。
- 使用CommentMark.CommentId屬性將開始和結(jié)束標(biāo)記設(shè)置為新建的批注的開始和結(jié)束標(biāo)記。
- 使用Paragraph.ChildObjects.Insert和Paragraph.ChildObjects.Add()方法將批注的開始和結(jié)束標(biāo)記分別插入到段落的開始和末尾。
- 使用Document.SaveToFile()方法保存Word文檔。
- 釋放資源。
代碼示例
from spire.doc import Document, CommentMark, CommentMarkType
# 創(chuàng)建Document對象
doc = Document()
# 載入Word文檔
doc.LoadFromFile("Sample.docx")
# 獲取文檔第一個節(jié)
section = doc.Sections.get_Item(0)
# 獲取節(jié)中第一個段落
paragraph = section.Paragraphs.get_Item(4)
# 添加一個批注到段落
comment = paragraph.AppendComment("Does this account for industries like manufacturing or healthcare where remote work isn't feasible?")
# 設(shè)置批注作者
comment.Format.Author = "Jane"
# 創(chuàng)建批注起始標(biāo)記和結(jié)束標(biāo)記,并設(shè)置為新建批注的開始和結(jié)束標(biāo)記
commentStart = CommentMark(doc, CommentMarkType.CommentStart)
commentEnd = CommentMark(doc, CommentMarkType.CommentEnd)
commentStart.CommentId = comment.Format.CommentId
commentEnd.CommentId = comment.Format.CommentId
# 將批注起始標(biāo)記和結(jié)束標(biāo)記分別插入段落開頭和結(jié)束位置
paragraph.ChildObjects.Insert(0, commentStart)
paragraph.ChildObjects.Add(commentEnd)
# 保存文檔
doc.SaveToFile("output/ParagraphComment.docx")
doc.Close()
結(jié)果

用Python在Word文檔中添加批注到文本
我們可以使用Document.FindString()方法從文檔中查找指定文本,然后將其獲取為文本區(qū)域,再使用添加批注到段落,并將批注開始和結(jié)束標(biāo)記插入到文本區(qū)域前后,來實現(xiàn)添加批注到指定文本。以下是操作步驟:
- 導(dǎo)入所需模塊。
- 創(chuàng)建Document對象,使用Document.LoadFromFile()方法載入Word文檔。
- 使用Document.FindString()方法從文檔查找需要添加批注的文本。
- 通過創(chuàng)建Comment對象新建一個批注。
- 使用Comment.Body.AddParagraph().Text屬性設(shè)置批注文本,并使用Comment.Format.Author屬性設(shè)置批注作者。
- 使用TextSelection.GetAsOneRange()方法將查找到的文本獲取為文本區(qū)域,然后使用TextRange.OwnerParagraph屬性獲取批注所在的段落。
- 使用CommentMark.CommentId屬性將開始和結(jié)束標(biāo)記設(shè)置為新建的批注的開始和結(jié)束標(biāo)記。
- 使用Paragraph.ChildObjects.Insert和Paragraph.ChildObjects.Add()方法將批注的開始和結(jié)束標(biāo)記分別插入到段落的開始和末尾。
- 使用Document.SaveToFile()方法保存Word文檔。
- 釋放資源。
代碼示例
from spire.doc import Document, Comment, CommentMark, CommentMarkType
# 創(chuàng)建Document對象
doc = Document()
# 載入Word文檔
doc.LoadFromFile("Sample.docx")
# 查找需要添加批注的文本
text = doc.FindString("over 60% of global companies", True, True)
# 創(chuàng)建批注對象,并設(shè)置批注的作者和內(nèi)容
comment = Comment(doc)
comment.Body.AddParagraph().Text = "Source for the 60% statistic? Is this global or region-specific?"
comment.Format.Author = "Sara"
# 將找到的文本獲取為文本區(qū)域,并獲取該文本區(qū)域所在的段落
textRange = text.GetAsOneRange()
paragraph = textRange.OwnerParagraph
# 將批注對象插入到段落中
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textRange) + 1, comment)
# 創(chuàng)建批注開始和結(jié)束標(biāo)記,并將其設(shè)置為新建批注的開始和結(jié)束標(biāo)記
commentStart = CommentMark(doc, CommentMarkType.CommentStart)
commentEnd = CommentMark(doc, CommentMarkType.CommentEnd)
commentStart.CommentId = comment.Format.CommentId
commentEnd.CommentId = comment.Format.CommentId
# 將批注開始和結(jié)束標(biāo)記分別插入到文本區(qū)域的前面和后面
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textRange), commentStart)
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textRange) + 1, commentEnd)
# 保存文檔
doc.SaveToFile("output/TextComment.docx")
doc.Close()
結(jié)果

用Python刪除Word文檔中的批注
Document.Comments屬性可以獲取文檔中的所有批注。我們可以使用Document.Comments.RemoveAt()刪除指定批注,或使用Document.Comments.Clear()刪除所有批注。以下是操作步驟:
- 導(dǎo)入所需模塊。
- 創(chuàng)建Document對象,使用Document.LoadFromFile()方法載入Word文檔。
- 使用Document.Comments.RemoveAt()刪除指定批注,或使用Document.Comments.Clear()刪除所有批注。
- 使用Document.SaveToFile()方法保存Word文檔。
- 釋放資源。
代碼示例
from spire.doc import Document
# 創(chuàng)建Document對象
doc = Document()
# 載入Word文檔
doc.LoadFromFile("output/ParagraphComment.docx")
# 刪除文檔指定批注
doc.Comments.RemoveAt(0)
# 刪除文檔所有批注
doc.Comments.Clear()
# 保存文檔
doc.SaveToFile("output/RemoveComment.docx")
doc.Close()
用Python在Word文檔中回復(fù)批注
通過新建Comment對象,并使用Comment.ReplyToComment()方法將其設(shè)置為指定批注的回復(fù)批注,我們可以實現(xiàn)對指定批注進(jìn)行回復(fù)。以下是操作步驟:
- 導(dǎo)入所需模塊。
- 創(chuàng)建Document對象,使用Document.LoadFromFile()方法載入Word文檔。
- 使用Document.Comments.get_Item()方法獲取指定批注。
- 通過創(chuàng)建Comment對象新建一個批注。
- 使用Comment.Body.AddParagraph().Text屬性設(shè)置批注文本,并使用Comment.Format.Author屬性設(shè)置批注作者。
- 使用Comment.ReplyToComment()方法將新建的批注設(shè)置為獲取的批注的回復(fù)。
- 使用Document.SaveToFile()方法保存Word文檔。
- 釋放資源。
代碼示例
from spire.doc import Document, Comment
# 創(chuàng)建Document對象
doc = Document()
# 載入Word文檔
doc.LoadFromFile("output/ParagraphComment.docx")
# 獲取批注
comment = doc.Comments.get_Item(0)
# 創(chuàng)建批注對象,設(shè)置文本及作者
reply = Comment(doc)
reply.Body.AddParagraph().Text = "It includes manufacturing and healthcare."
reply.Format.Author = "Peter"
# 將新建的批注設(shè)置為獲取的批注的回復(fù)
comment.ReplyToComment(reply)
# 保存文檔
doc.SaveToFile("output/CommentReply.docx")
doc.Close()
結(jié)果

以上就是使用Python在Word文檔中添加,刪除和回復(fù)批注的詳細(xì)內(nèi)容,更多關(guān)于Python Word批注操作的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實現(xiàn)PDF轉(zhuǎn)Word的方法詳解
由于PDF的文件大多都是只讀文件,有時候為了滿足可以編輯的需要通??梢詫DF文件直接轉(zhuǎn)換成Word文件進(jìn)行操作。本文為大家整理了一些實現(xiàn)方法,希望對大家有所幫助2023-02-02
python接口自動化(十七)--Json 數(shù)據(jù)處理---一次爬坑記(詳解)
這篇文章主要介紹了python Json 數(shù)據(jù)處理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
10分鐘教你用python動畫演示深度優(yōu)先算法搜尋逃出迷宮的路徑
Python如何把不同類型數(shù)據(jù)的json序列化
Python實現(xiàn)wav和pcm的轉(zhuǎn)換方式
使paramiko庫執(zhí)行命令時在給定的時間強(qiáng)制退出功能的實現(xiàn)
Pycharm遠(yuǎn)程調(diào)試和MySQL數(shù)據(jù)庫授權(quán)問題

