
目錄 (TOC) 是 Word 文件中的一個重要功能,特別是對於長篇文件如報告、論文和電子書。它使 Word 文件更易讀,並允許讀者快速瀏覽文件中的不同部分。在這篇文章中,您將學習如何使用 Python 程式化地自動化 Word 文件中的目錄。我們將討論如何在 Word 文件中創建目錄、在現有文件中插入目錄或從文件中提取目錄的步驟。
本文涵蓋以下主題:
使用 Python 的 Word 庫操作 TOC
要在 Word 文件中使用 Python 操作 TOC,我們將使用 Aspose.Words for Python 庫。它允許開發者在 Python 應用程序中創建、修改和自動化 Word 文件。讓我們先設置好環境。
在進入代碼之前,請確保您已安裝 Aspose.Words for Python。您可以 下載套件 或使用以下 pip 命令在終端中安裝 API:
pip install aspose-words
這將下載並安裝 Aspose.Words for Python 庫及其依賴項。
使用 Python 在 Word 中創建目錄
請按照以下步驟使用 Python 向 Word 文件添加目錄。
- 創建 Document 類的實例。
- 使用 Document 對象創建 DocumentBuilder 類的實例。
- 使用 insert_table_of_contents() 方法插入目錄。
- 使用 style_identifier 屬性指定標題樣式。
- 使用 update_fields() 方法更新字段。
- 使用 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") |

使用 Python 在 Word 中添加目錄
在現有 Word 文件中創建目錄
您也可以通過以下步驟在現有的 Word 文件中插入目錄:
- 使用 Document 類加載現有的 Word 文件。
- 創建 DocumentBuilder 類的實例,並用先前創建的 Document 對象初始化。
- 使用 insert_table_of_contents() 方法插入目錄。
- 使用 update_fields() 方法更新字段。
- 使用 save() 方法保存 Word 文件。
以下代碼示例展示了如何在現有的 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"); |

在現有 Word 文件中創建目錄
從 Word 文件中提取目錄
您可以通過以下步驟從 Word 文件中提取目錄字段:
- 使用 Document 類加載 Word 文件。
- 遍歷 doc.range.fields 集合。
- 檢查字段類型是否為 FIELD_HYPERLINK。
- 檢查字段是否屬於目錄部分。
- 從字段中檢索信息並打印。
以下代碼示例展示了如何使用 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("------------------") | |
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) 在 Word 中 – 免費資源
- 閱讀 操作目錄 部分的官方文檔,了解如何在 Word 中添加、更新或刪除目錄。
除了操作 Word 文件中的目錄外,您還可以通過以下資源探索更多 Aspose.Words for Python 的功能:
結論
在這篇文章中,您學會了如何以程式化方式操作 Word 文件中的目錄 (TOC)。通過遵循本文所述的步驟,您可以自動化創建和管理長篇文件的過程。Aspose.Words for Python 使您能夠高效地管理 Word 文件,並通過結構良好的目錄提高其可用性。如果您有任何問題或需要進一步幫助,請隨時在我們的 免費支持論壇 上聯繫我們。