Word 文書の脚注は、文書の内容を補足し、参考情報や引用を提供するための便利な機能です。脚注を利用することで、複雑な概念の詳細な説明を追加したり、主張を裏付ける出典を明示したり、読者にとって興味深い補足情報を提示したりできます。学術論文や書籍、その他の引用や説明を要する文書を作成する際に、脚注はレイアウトをすっきりと保ちながら補足情報を提供するのに役立ちます。本記事では、Spire.Doc for Python を使用して Word 文書に脚注を挿入・削除する方法を解説します。 Python で特定の段落に脚注を挿入する Python で特定のテキストに脚注を挿入する 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 で特定の段落に脚注を挿入する Spire.Doc for Python が提供する Paragraph.AppendFootnote(FootnoteType.Footnote) メソッドを使用すると、特定の段落に簡単に脚注を追加できます。手順は以下の通りです。 Document クラスのオブジェクトを作成します。 Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。 Document.Section[int] プロパティを用いて特定のセクションを取得し、Section.Paragraphs[int] プロパティを使ってセクション内の特定の段落を取得します。 Paragraph.AppendFootnote(FootnoteType.Footnote) メソッドを使用して、段落の末尾に脚注を追加します。 脚注の本文を設定し、脚注のテキストおよび参照マークのフォントや色を調整します。 Document.SaveToFile() メソッドを使用して、結果の文書を保存します。 Python from spire.doc import * from spire.doc.common import * # ドキュメントインスタンスを作成 document = Document() # サンプルのWordドキュメントを読み込む document.LoadFromFile("Sample.docx") # 最初のセクションを取得 section = document.Sections.get_Item(0) # セクション内の指定された段落を取得 paragraph = section.Paragraphs.get_Item(5) # 段落の最後に脚注を追加 footnote = paragraph.AppendFootnote(FootnoteType.Footnote) # 脚注のテキストコンテンツを設定 text = footnote.TextBody.AddParagraph().AppendText("Agrawal, A., Gans, J., & Goldfarb, A. (2018). 予測マシン: 人工知能の簡単な経済学。") # テキストのフォントと色を設定 text.CharacterFormat.FontName = "Yu Mincho" text.CharacterFormat.FontSize = 12 text.CharacterFormat.TextColor = Color.get_DarkBlue() # 脚注参照マークのフォントと色を設定 footnote.MarkerCharacterFormat.FontName = "Calibri" footnote.MarkerCharacterFormat.FontSize = 15 footnote.MarkerCharacterFormat.Bold = True footnote.MarkerCharacterFormat.TextColor = Color.get_DarkCyan() # 結果のドキュメントを保存 document.SaveToFile("output/Wordの段落に脚注を挿入.docx") document.Close() Python で特定のテキストに脚注を挿入する 特定のテキストに脚注を追加する場合は、文書内の該当テキストを検索し、そのテキストが属する段落内の位置を取得した後に、脚注を挿入します。手順は以下の通りです。 Document クラスのオブジェクトを作成します。 Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。 Document.FindString() メソッドを使って特定のテキストを検索します。 TextSelection.GetAsOneRange() メソッドを使用して、検索結果を 1 つのテキスト範囲として取得します。 TextRange.OwnerParagraph プロパティを利用して、テキスト範囲が属する段落を取得します。 Paragraph.ChildObjects.IndexOf() メソッドを使用して、段落内でのテキスト範囲の位置を特定します。 Paragraph.AppendFootnote(FootnoteType.Footnote) メソッドで脚注を追加し、Paragraph.ChildObjects.Insert() メソッドで特定のテキストの後に脚注を挿入します。 脚注の本文を設定し、脚注のテキストおよび参照マークのフォントや色を調整します。 Document.SaveToFile() メソッドを使用して、結果の文書を保存します。 Python from spire.doc import * from spire.doc.common import * # ドキュメントインスタンスを作成 document = Document() # サンプルのWordドキュメントを読み込む document.LoadFromFile("Sample.docx") # 特定のテキストを検索 selection = document.FindString("アルゴリズムのバイアス", False, True) # 検索したテキストを1つのテキスト範囲として取得 textRange = selection.GetAsOneRange() # テキスト範囲が存在する段落を取得 paragraph = textRange.OwnerParagraph # 段落内でのテキスト範囲のインデックス位置を取得 index = paragraph.ChildObjects.IndexOf(textRange) # 段落に脚注を追加 footnote = paragraph.AppendFootnote(FootnoteType.Footnote) # テキスト範囲の後に脚注を挿入 paragraph.ChildObjects.Insert(index + 1, footnote) # 脚注のテキストコンテンツを設定 text = footnote.TextBody.AddParagraph().AppendText("アルゴリズムのバイアス: AIシステムがデータに基づいて判断を下す際に、特定の偏りが生じる現象。これにより、誤った結果や不公平な結果が生じることがあります。") # テキストのフォントと色を設定 text.CharacterFormat.FontName = "Yu Mincho" text.CharacterFormat.FontSize = 12 text.CharacterFormat.TextColor = Color.get_DarkBlue() # 脚注参照マークのフォントと色を設定 footnote.MarkerCharacterFormat.FontName = "Calibri" footnote.MarkerCharacterFormat.FontSize = 15 footnote.MarkerCharacterFormat.Bold = True footnote.MarkerCharacterFormat.TextColor = Color.get_DarkGreen() # 結果のドキュメントを保存 document.SaveToFile("output/Wordのテキストに脚注を挿入.docx") document.Close() Python で Word 文書の脚注を削除する 不要になった脚注を削除することで、文書をより整理された状態にできます。脚注を削除する手順は以下の通りです。 Document クラスのオブジェクトを作成します。 Document.LoadFromFile() メソッドを使用して Word 文書を読み込みます。 Document.Sections[int] プロパティを使って特定のセクションを取得します。 取得したセクション内の各段落をループ処理し、脚注を検索します。 Paragraph.ChildObjects.RemoveAt() メソッドを使用して脚注を削除します。 Document.SaveToFile() メソッドを使用して、結果の文書を保存します。 Python from spire.doc import * from spire.doc.common import * # ドキュメントインスタンスを作成 document = Document() # サンプルのWordドキュメントを読み込む document.LoadFromFile("output/Wordの段落に脚注を挿入.docx") # ドキュメントの最初のセクションを取得 section = document.Sections.get_Item(0) # セクション内の段落をループ処理 for y in range(section.Paragraphs.Count): para = section.Paragraphs.get_Item(y) index = -1 i = 0 cnt = para.ChildObjects.Count while i < cnt: pBase = para.ChildObjects.get_Item(i) if isinstance(para.ChildObjects.get_Item(i), ParagraphBase) else None if isinstance(pBase, Footnote): index = i break i += 1 if index > -1: # 段落から脚注を削除 para.ChildObjects.RemoveAt(index) # 結果のドキュメントを保存 document.SaveToFile("output/Wordの脚注を削除.docx") document.Close() 一時ライセンスを申請する 結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。 にお問い合わせ、30 日間有効な一時ライセンスを取得してください。