Word 文書のコメント機能は、コラボレーションやフィードバックにおいて非常に重要な役割を果たします。コメントを活用することで、意見の共有、提案、説明、議論をスムーズに行うことができます。アイデアを伝えたり、既存のコメントに返信したり、不要なコメントを削除したりすることで、ドキュメントの作業効率を大幅に向上させることができます。本記事では、Spire.Doc for Python を使用して Python で Word 文書のコメントを追加、削除、返信する方法について解説します。
- Python で Word 文書の段落にコメントを追加する
- Python で Word 文書のテキストにコメントを追加する
- Python で Word 文書のコメントを削除する
- Python で Word 文書のコメントに返信する
Spire.Doc for Python のインストール
この操作には、Spire.Doc for Python と plum-dispatch v1.7.4 が必要です。これらは、Spire.Doc for Python の公式ウェブサイトから手動でダウンロードするか、以下の pip コマンドでインストールできます。
pip install Spire.Doc
Python で Word 文書の段落にコメントを追加する
Spire.Doc for Python では、Paragraph.AppendComment() メソッドを使用して、指定した段落にコメントを追加できます。コメント対象のテキスト範囲は、コメントの開始マークと終了マークを設定することで制御します。以下の手順で段落にコメントを追加します。
- Document クラスのオブジェクトを作成し、Document.LoadFromFile() メソッドで Word 文書を読み込みます。
- Document.Sections.get_Item() メソッドを使用して最初のセクションを取得します。
- Section.Paragraphs.get_Item() メソッドを使用して、そのセクションの最初の段落を取得します。
- Paragraph.AppendComment() メソッドで、段落にコメントを追加します。
- Comment.Format.Author プロパティを設定して、コメントの作成者を指定します。
- コメントの開始マーク (CommentMark) と終了マーク (CommentMark) を作成し、それらを CommentMark.CommentId プロパティを通じてコメントの開始・終了位置として設定します。
- Paragraph.ChildObjects.Insert() メソッドを使用して、コメントの開始マークと終了マークをそれぞれ段落の先頭と末尾に挿入します。
- Document.SaveToFile() メソッドで Word 文書を保存します。
- Python
from spire.doc import Document, CommentMark, CommentMarkType
# Document クラスのオブジェクトを作成し、Word 文書を読み込みます
doc = Document()
doc.LoadFromFile("Sample.docx")
# 最初のセクションを取得します
section = doc.Sections.get_Item(0)
# 4 番目の段落を取得します
paragraph = section.Paragraphs.get_Item(3)
# 段落にコメントを追加します
comment = paragraph.AppendComment("この部分はさらに説明が必要です。")
# コメントの著者を設定します
comment.Format.Author = "大輝"
# コメントの開始マークと終了マークを作成し、作成したコメントの開始マークと終了マークとして設定します
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/段落へのコメント.docx")
doc.Close()
Python で Word 文書のテキストにコメントを追加する
Spire.Doc for Python では、指定したテキストを検索し、その部分にコメントを追加することも可能です。以下の手順で、特定のテキストにコメントを追加します。
- Document クラスのオブジェクトを作成し、Document.LoadFromFile() メソッドで Word 文書を読み込みます。
- Document.FindString() メソッドを使用して、コメントを追加したいテキストを検索します。
- Comment クラスのオブジェクトを作成し、Comment.Body.AddParagraph().Text プロパティでコメント内容を設定し、Comment.Format.Author プロパティで作成者を指定します。
- TextSelection.GetAsOneRange() メソッドを使用して、検索したテキストを TextRange として取得し、TextRange.OwnerParagraph プロパティを使ってそのテキストが属する段落を取得します。
- Paragraph.ChildObjects.Insert() メソッドで、検索したテキストの後にコメントを挿入します。
- コメントの開始マーク (CommentMark) と終了マーク (CommentMark) を作成し、それらを CommentMark.CommentId プロパティを通じてコメントの開始・終了位置として設定します。
- Paragraph.ChildObjects.Insert() メソッドを使用して、検索したテキストの前後にコメントの開始マークと終了マークを挿入します。
- Document.SaveToFile() メソッドで Word 文書を保存します。
- Python
from spire.doc import Document, Comment, CommentMark, CommentMarkType
# Document クラスのオブジェクトを作成し、Word 文書を読み込みます
doc = Document()
doc.LoadFromFile("Sample.docx")
# コメントを追加するテキストを検索します
text = doc.FindString("抗酸化作用", True, True)
# コメントを作成し、コメントの内容と著者を設定します
comment = Comment(doc)
comment.Body.AddParagraph().Text = "抗酸化作用について、さらに詳しく説明する必要があります。"
comment.Format.Author = "奈々"
# 検索したテキストをテキスト範囲として取得し、それが属する段落を取得します
range = text.GetAsOneRange()
paragraph = range.OwnerParagraph
# コメントを段落に追加します
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(range) + 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(range), commentStart)
paragraph.ChildObjects.Insert(paragraph.ChildObjects.IndexOf(range) + 1, commentEnd)
# 文書を保存します
doc.SaveToFile("output/テキストへのコメント.docx")
doc.Close()
Python で Word 文書のコメントを削除する
Spire.Doc for Python では、Document.Comments.RemoveAt() メソッドを使用して特定のコメントを削除できます。また、Document.Comments.Clear() メソッドを使用すれば、すべてのコメントを削除できます。以下の手順でコメントを削除します。
- Document クラスのオブジェクトを作成し、Document.LoadFromFile() メソッドで Word 文書を読み込みます。
- Document.Comments.RemoveAt() メソッドを使用して特定のコメントを削除する、または Document.Comments.Clear() メソッドですべてのコメントを削除します。
- Document.SaveToFile() メソッドで Word 文書を保存します。
- Python
from spire.doc import Document
# Document クラスのオブジェクトを作成し、Word 文書を読み込みます
doc = Document()
doc.LoadFromFile("Sample1.docx")
# 2 番目のコメントを削除します
doc.Comments.RemoveAt(1)
# すべてのコメントを削除します
# doc.Comments.Clear()
# 文書を保存します
doc.SaveToFile("output/コメントの削除.docx")
doc.Close()
Python で Word 文書のコメントに返信する
Spire.Doc for Python を使用すると、Comment.ReplyToComment(Comment) メソッドを利用して、特定のコメントに対して返信を追加できます。以下の手順でコメントに返信を追加します。
- Document クラスのオブジェクトを作成し、Document.LoadFromFile() メソッドで Word 文書を読み込みます。
- Document.Comments.get_Item() メソッドを使用して、対象のコメントを取得します。
- Comment クラスのオブジェクトを作成し、Comment.Body.AddParagraph().Text プロパティでコメント内容を設定し、Comment.Format.Author プロパティで作成者を指定します。
- Comment.ReplyToComment() メソッドを使用して、作成したコメントを取得したコメントへの返信として設定します。
- Document.SaveToFile() メソッドで Word 文書を保存します。
- Python
from spire.doc import Document, Comment
# Document クラスのオブジェクトを作成し、Word 文書を読み込みます
doc = Document()
doc.LoadFromFile("output/テキストへのコメント.docx")
# コメントを取得します
comment = doc.Comments.get_Item(0)
# 返信コメントを作成し、内容と著者を設定します
reply = Comment(doc)
reply.Body.AddParagraph().Text = "これは次のバージョンに追加されます。"
reply.Format.Author = "優花"
# 取得したコメントへの返信として作成したコメントを設定します
comment.ReplyToComment(reply)
# 文書を保存します
doc.SaveToFile("output/コメントへの返信.docx")
doc.Close()
一時ライセンスを申請する
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。