文書の共同作業やレビューにおいて、コメントの効率的な管理はチームの生産性向上に不可欠です。プログラムによるコメント操作の自動化を行うことで、手作業によるミスを防ぎ、バージョン間のコメント追跡や過去のフィードバックの一括処理などの複雑な要件を実現できます。たとえば、コメントの自動追加により標準化チェックの結果を文書に埋め込み、不要なコメントの削除で文書を整理し、コメントへの返信機能を活用して完全なレビュー対話を構築できます。特に、ソフトウェア開発文書のレビューや学術論文の改訂など、厳密なバージョン管理が求められる場面で有用です。
本記事では、Pythonを使用してWord文書にコメントを追加・削除・返信する方法を、手順とコードサンプルとともに紹介します。
- PythonでWord文書の段落にコメントを追加する
- PythonでWord文書の特定のテキストにコメントを追加する
- PythonでWord文書のコメントを削除する
- PythonでWord文書のコメントに返信する
本記事では、無料のFree Spire.Doc for Pythonを使用します。PyPIよりインストールできます。
pip install spire.doc.free
PythonでWord文書の段落にコメントを追加する
Paragraph.AppendComment()
メソッドを使用すると、Word文書の段落にコメントを追加できます。コメントの作成後、Comment.Format.Author
プロパティでコメントの作成者を設定し、コメントの開始・終了マークを指定することで、適切なコメントの管理が可能になります。
手順
- 必要なモジュールをインポートする。
-
Document
オブジェクトを作成し、Document.LoadFromFile()
メソッドでWord文書を読み込む。 -
Document.Sections.get_Item()
メソッドで対象のセクションを取得する。 -
Section.Paragraphs.get_Item()
メソッドで対象の段落を取得する。 -
Paragraph.AppendComment()
メソッドでコメントを追加する。 -
Comment.Format.Author
プロパティでコメントの作成者を設定する。 -
CommentMark
オブジェクトを作成し、開始・終了マークを設定する。 -
CommentMark.CommentId
プロパティでコメントIDを設定する。 -
Paragraph.ChildObjects.Insert()
およびParagraph.ChildObjects.Add()
メソッドでコメントの開始・終了マークを適切な位置に挿入する。 -
Document.SaveToFile()
メソッドで文書を保存する。 - リソースを解放する。
コードサンプル
from spire.doc import Document, CommentMark, CommentMarkType
# Documentオブジェクトを作成
doc = Document()
# Word文書を読み込む
doc.LoadFromFile("Sample.docx")
# 文書の最初のセクションを取得
section = doc.Sections.get_Item(0)
# セクション内の第5段落を取得
paragraph = section.Paragraphs.get_Item(4)
# 段落にコメントを追加
comment = paragraph.AppendComment("製造業や医療業界など、リモートワークが難しい分野も考慮されていますか?")
# コメントの作成者を設定
comment.Format.Author = "Jane"
# コメントの開始・終了マークを作成し、設定
commentStart = CommentMark(doc, CommentMarkType.CommentStart)
commentEnd = CommentMark(doc, CommentMarkType.CommentEnd)
commentStart.CommentId = comment.Format.CommentId
commentEnd.CommentId = comment.Format.CommentId
# コメントの開始・終了マークを段落内の適切な位置に挿入
paragraph.ChildObjects.Insert(0, commentStart)
paragraph.ChildObjects.Add(commentEnd)
# 文書を保存
doc.SaveToFile("output/ParagraphComment.docx")
doc.Close()
PythonでWord文書の特定のテキストにコメントを追加する
Document.FindString()
メソッドを使用して特定のテキストを検索し、そのテキスト範囲を取得後、適切にコメントを挿入します。
コードサンプル
from spire.doc import Document, Comment, CommentMark, CommentMarkType
# Documentオブジェクトを作成
doc = Document()
# Word文書を読み込む
doc.LoadFromFile("Sample.docx")
# 指定のテキストを検索
text = doc.FindString("over 60% of global companies", True, True)
# コメントを作成し、作成者と内容を設定
comment = Comment(doc)
comment.Body.AddParagraph().Text = "この60%の統計の出典は?これは世界全体のデータですか、それとも特定地域のものですか?"
comment.Format.Author = "Sara"
# 検索したテキスト範囲を取得
textRange = text.GetAsOneRange()
paragraph = textRange.OwnerParagraph
# コメントを段落に挿入
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textRange) + 1, comment)
# コメントの開始・終了マークを作成し、設定
commentStart = CommentMark(doc, CommentMarkType.CommentStart)
commentEnd = CommentMark(doc, CommentMarkType.CommentEnd)
commentStart.CommentId = comment.Format.CommentId
commentEnd.CommentId = comment.Format.CommentId
# コメントの開始・終了マークをテキストの前後に挿入
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textRange), commentStart)
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(textRange) + 1, commentEnd)
# 文書を保存
doc.SaveToFile("output/TextComment.docx")
doc.Close()
PythonでWord文書のコメントを削除する
コードサンプル
from spire.doc import Document
doc = Document()
doc.LoadFromFile("output/ParagraphComment.docx")
# 指定のコメントを削除
doc.Comments.RemoveAt(0)
# すべてのコメントを削除
doc.Comments.Clear()
# 文書を保存
doc.SaveToFile("output/RemoveComment.docx")
doc.Close()
PythonでWord文書のコメントに返信する
コードサンプル
from spire.doc import Document, Comment
doc = Document()
doc.LoadFromFile("output/ParagraphComment.docx")
# 既存のコメントを取得
comment = doc.Comments.get_Item(0)
# 新しいコメントを作成し、返信として設定
reply = Comment(doc)
reply.Body.AddParagraph().Text = "製造業や医療業界も含まれます。"
reply.Format.Author = "Peter"
comment.ReplyToComment(reply)
# 文書を保存
doc.SaveToFile("output/CommentReply.docx")
doc.Close()