Create TOC in Word

目次(TOC)は、特にレポート、論文、eBookなどの長文のWord文書において、必須の機能です。目次はWord文書を読みやすくし、読者が文書のさまざまなセクションをすばやくナビゲートできるようにします。この投稿では、Pythonを使用してWord文書の目次を自動化する方法を学びます。Word文書に目次を作成する手順、既存の文書に目次を挿入する方法、およびPythonで文書から目次を抽出する方法について説明します。

この記事では、以下のトピックについて説明します:

TOCを操作するためのPython Wordライブラリ

Pythonを使用してWord文書の目次を操作するために、Aspose.Words for Pythonライブラリを使用します。このライブラリを使用すると、PythonアプリケーションでWord文書を作成、修正、自動化することができます。環境のセットアップから始めましょう。

コードを実行する前に、Aspose.Words for Pythonがインストールされていることを確認してください。パッケージをダウンロードするか、PyPIから以下のpipコマンドを使用してAPIをインストールできます:

pip install aspose-words

これにより、Aspose.Words for Pythonライブラリとその依存関係がダウンロードおよびインストールされます。

PythonでWordに目次を作成する

Pythonを使用してWord文書に目次を追加するには、以下の手順に従ってください。

  1. **Document**クラスのインスタンスを作成します。
  2. _Document_オブジェクトを使用して**DocumentBuilder**クラスのインスタンスを作成します。
  3. **insert_table_of_contents()**メソッドを使用して目次を挿入します。
  4. style_identifierプロパティを使用して見出しスタイルを指定します。
  5. **update_fields()**メソッドを使用してフィールドを更新します。
  6. **save()**メソッドを使用してWord文書を保存します。

以下のコードサンプルは、Pythonを使用してWord文書に目次を作成する方法を示しています。

# This code example shows how to add a Table of Contents in a Word document.
# Create a document
doc = aw.Document()
# Initialize a document builder
builder = aw.DocumentBuilder(doc)
# Insert table of contents
builder.insert_table_of_contents("\\o \"1-3\" \\h \\z \\u")
# Start the actual document content on the second page.
builder.insert_break(aw.BreakType.PAGE_BREAK)
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.writeln("Heading 1")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING2
builder.writeln("Heading 1.1")
builder.writeln("Heading 1.2")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING1
builder.writeln("Heading 2")
builder.writeln("Heading 3")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING2
builder.writeln("Heading 3.1")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING3
builder.writeln("Heading 3.1.1")
builder.writeln("Heading 3.1.2")
builder.writeln("Heading 3.1.3")
builder.paragraph_format.style_identifier = aw.StyleIdentifier.HEADING2
builder.writeln("Heading 3.2")
builder.writeln("Heading 3.3")
# The newly inserted table of contents will be initially empty.
# It needs to be populated by updating the fields in the document.
doc.update_fields()
# Save the document
doc.save("insert_table_of_contents.docx")
Create a Table of Contents in Word using Python.

Pythonを使用してWordに目次を追加する

既存のWord文書に目次を作成する

既存のWord文書に目次を挿入することもできます。以下の手順に従ってください:

  1. **Document**クラスを使用して既存のWord文書をロードします。
  2. **DocumentBuilder**クラスのインスタンスを作成し、先に作成した_Document_オブジェクトを初期化します。
  3. **insert_table_of_contents()**メソッドを使用して目次を挿入します。
  4. **update_fields()**メソッドを使用してフィールドを更新します。
  5. **save()**メソッドを使用してWord文書を保存します。

以下のコードサンプルは、Pythonを使用して既存のWord文書に目次を作成する方法を示しています。

# This code example shows how to insert a Table of Contents in an existing Word document.
# Load an existing Word document
doc = Document("toc_sample.docx");
builder = DocumentBuilder(doc);
# Insert a table of contents at the beginning of the document.
builder.insert_table_of_contents("\\o \"1-3\" \\h \\z \\u");
# The newly inserted table of contents will be initially empty.
# It needs to be populated by updating the fields in the document.
doc.update_fields();
# Save the document
doc.save("InsertTOC_out.docx");
Create a Table of Contents in an Existing Word Document.

既存のWord文書に目次を作成する

PythonでWord文書から目次を抽出する

以下の手順に従って、Word文書の目次からフィールドを抽出できます:

  1. **Document**クラスを使用してWord文書をロードします。
  2. doc.range.fieldsコレクションをループします。
  3. フィールドタイプがFIELD_HYPERLINKであるかを確認します。
  4. フィールドが目次セクションに含まれているかを確認します。
  5. フィールドから情報を取得して表示します。

以下のコードサンプルは、Pythonを使用してWord文書から目次を抽出する方法を示しています。

# This code example shows how to extract a Table of Contents from a Word document.
# Load an existing Word document
doc = aw.Document(InsertTOC_out.docx")
# Loop through all the fields
for field in doc.range.fields:
# Check if it is FIELD_HYPERLINK
if (field.type == aw.fields.FieldType.FIELD_HYPERLINK):
hyperlink = field.as_field_hyperlink()
# Check if it in TOC section
if (hyperlink.sub_address != None and hyperlink.sub_address.find("_Toc") == 0):
tocItem = field.start.get_ancestor(aw.NodeType.PARAGRAPH).as_paragraph()
print(tocItem.to_string(aw.SaveFormat.TEXT).strip())
print("------------------")
# Print
bm = doc.range.bookmarks.get_by_name(hyperlink.sub_address)
pointer = bm.bookmark_start.get_ancestor(aw.NodeType.PARAGRAPH).as_paragraph()
print(pointer.to_string(aw.SaveFormat.TEXT))
Heading 1       1
------------------
Heading 1

Heading2        1
------------------
Heading2

Another Heading 1
------------------
Another Heading

無料ライセンスを取得する

無料の一時ライセンスを取得して、評価制限なしでWord文書に目次を作成できます。

TOC in Word – 無料リソース

  • 公式ドキュメントの目次の操作セクションを読んで、WordでのTOCの追加、更新、削除についてさらに学びます。

Word文書の目次操作以外にも、以下のリソースを使用してAspose.Words for Pythonのその他の機能を探索してください:

結論

この記事では、プログラムを使用してWord文書の目次(TOC)を操作する方法を学びました。この記事で説明されている手順に従うことで、長文文書の作成と管理のプロセスを自動化できます。Aspose.Words for Pythonは、Word文書を効率的に管理し、よく構造化された目次を使用してその使いやすさを向上させるための強力なツールを提供します。質問やさらなる支援が必要な場合は、無料サポートフォーラムでお気軽にお問い合わせください。

See Also