创建 Word 文档的目录有助于提升文档的可导航性和易读性。它如同文档的导航指南,使得读者能迅速概览文档结构,把握内容框架。帮助用户可以轻松跳转至文档内的任意章节,尤其对于长篇报告、论文或手册极为有用。这不仅节省了读者定位信息的时间,也增强了文档的专业性和读者的使用体验。此外,目录便于维护更新,文档结构调整后可快速更新目录以反映最新内容布局,保证文档的连贯性和准确性。本文将介绍如何使用 Spire.Doc for Python 在 Python 项目中给新建的 Word 文档创建目录。
安装 Spire.Doc for Python
本教程需要用到 Spire.Doc for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Doc
如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.Doc for Python
Python 利用标题样式创建目录
利用标题样式创建目录是一种在 Word 文档中默认自动生成目录的方法,通过使用不同级别的标题样式来标识文档中的章节和子章节,然后利用 Word 的目录功能自动生成目录内容。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.AddSection() 方法添加一个节。
- 使用 Section.AddParagraph() 方法添加一个段落。
- 使用 Paragraph.AppendTOC(int lowerLevel, int upperLevel) 方法创建一个目录对象。
- 创建一个 CharacterFormat 字符格式对象并设置字体。
- 使用 Paragraph.ApplyStyle(BuiltinStyle.Heading1) 方法给段落应用标题样式。
- 使用 Paragraph.AppendTex() 方法添加文本内容。
- 使用 TextRange.ApplyCharacterFormat() 方法给文本设置字符格式。
- 使用 Document.UpdateTableOfContents() 方法更新目录。
- 使用 Document.SaveToFile() 方法保存到文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个新的文档对象
doc = Document()
# 在文档中添加一个节
section = doc.AddSection()
# 添加目录段落
TOCparagraph = section.AddParagraph()
TOCparagraph.AppendTOC(1, 3)
# 创建字符格式对象并设置字体
characterFormat1 = CharacterFormat(doc)
characterFormat1.FontName = "微软雅黑"
# 创建另一个字符格式对象并设置字体、字体大小
characterFormat2 = CharacterFormat(doc)
characterFormat2.FontName = "微软雅黑"
characterFormat2.FontSize = 12
# 添加标题1样式的段落
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading1)
# 添加文本并应用字符格式
textRange1 = paragraph.AppendText("概述")
textRange1.ApplyCharacterFormat(characterFormat1)
# 添加普通内容
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("Spire.Doc for Python 是一款专业的 Python Word 开发组件,开发人员可以使用它轻松将 Word 文档的创建、读取、编辑和转换等功能集成到自己的 Python 应用程序中。作为一款完全独立的组件,Spire.Doc for Python 的运行环境无需安装 Microsoft Word。同时兼容大部分国产操作系统,能够在中标麒麟和中科方德等国产操作系统中正常运行。")
textRange2.ApplyCharacterFormat(characterFormat2)
# 添加标题1样式的段落
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading1)
textRange1 = paragraph.AppendText("主要功能")
textRange1.ApplyCharacterFormat(characterFormat1)
# 添加标题2样式的段落
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading2)
textRange1 = paragraph.AppendText("仅需 Spire.Doc,无需 Microsoft Office Automation")
textRange1.ApplyCharacterFormat(characterFormat1)
# 添加普通内容
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("Spire.Doc for Python 是一个完全独立的 .NET Word 类库,它所运行的系统无需安装 Microsoft Office。Spire.Doc for Python 和 Microsoft Word Automation 相比,优点在于稳定性高,运行速度快和可扩展性强。")
textRange2.ApplyCharacterFormat(characterFormat2)
# 添加标题3样式的段落
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading3)
textRange1 = paragraph.AppendText("Word 版本")
textRange1.ApplyCharacterFormat(characterFormat1)
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("Word97-03 Word2007 Word2010 Word2013 Word2016 Word2019")
textRange2.ApplyCharacterFormat(characterFormat2)
# 添加标题2样式的段落
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading2)
textRange1 = paragraph.AppendText("高质量的文档转换")
textRange1.ApplyCharacterFormat(characterFormat1)
# 添加普通内容
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("Spire.Doc for Python 能帮助用户将 Word 文件保存在流中,还支持将 Word 文件与 XML、RTF、EMF、TXT、XPS、EPUB、HTML、SVG 和 ODT 等格式文件之间的双向转换。同时,它还支持将 Word 文件转换为 PDF 文件和 OFD,HTML 文件转换为图像文件。")
textRange2.ApplyCharacterFormat(characterFormat2)
# 添加标题2样式的段落
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading2)
extRange1 = paragraph.AppendText("多样化的 Word 文档功能")
textRange1.ApplyCharacterFormat(characterFormat1)
# 添加普通内容
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("Spire.Doc for Python 的一个常用功能就是动态创建一个全新的 Word 文档。并支持几乎所有的 Word 文档元素,它们主要包括页面、节、页眉、页脚、数字签名、脚注、尾注、段落、项目符号和编号、表格、文本、域、超链接、书签、注释、图片、样式、背景设置、打印功能、文档设置和文档保护。同时,也支持形状、文本框、图片、OLE 对象、公式和内容控件。")
textRange2.ApplyCharacterFormat(characterFormat2)
# 更新目录
doc.UpdateTableOfContents()
# 保存文档
doc.SaveToFile("利用标题样式创建目录.docx", FileFormat.Docx2016)
# 释放资源
doc.Dispose()
Python 利用大纲层级样式创建目录
在 Word 文档中,利用大纲层级样式也可以创建目录。可以使用 ParagraphFormat.OutlineLevel 属性来为段落指定在大纲中的层级样式。然后,通过 TableOfContent.SetTOCLevelStyle() 方法将这些大纲层级样式应用到创建目录的规则中。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.AddSection() 方法添加一个节。
- 创建一个 ParagraphStyle 对象,并使用 ParagraphStyle.ParagraphFormat.OutlineLevel = OutlineLevel.Level1 设置大纲层级。
- 使用 Document.Styles.Add() 方法将创建好的 ParagraphStyle 对象添加到文档中。
- 使用 Section.AddParagraph() 方法添加一个段落。
- 使用 Paragraph.AppendTOC(int lowerLevel, int upperLevel) 方法创建一个目录对象。
- 将使用标题样式创建目录的默认设置改为 False, TableOfContent.UseHeadingStyles = false。
- 使用 TableOfContent.SetTOCLevelStyle(int levelNumber, string styleName) 方法将大纲层级样式应用到目录规则中。
- 创建一个 CharacterFormat 字符格式对象并设置字体。
- 使用 Paragraph.ApplyStyle(ParagraphStyle.Name) 方法给段落应用样式。
- 使用 Paragraph.AppendTex() 方法添加文本内容。
- 使用 TextRange.ApplyCharacterFormat() 方法给文本设置字符格式。
- 使用 Document.UpdateTableOfContents() 方法更新目录。
- 使用 Document.SaveToFile() 方法保存到文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个新的文档对象
doc = Document()
# 在文档中添加一个节
section = doc.AddSection()
# 定义大纲级别1
titleStyle1 = ParagraphStyle(doc)
titleStyle1.Name = "T1S"
titleStyle1.ParagraphFormat.OutlineLevel = OutlineLevel.Level1
titleStyle1.CharacterFormat.Bold = True
titleStyle1.CharacterFormat.FontName = "微软雅黑"
titleStyle1.CharacterFormat.FontSize = 18
titleStyle1.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left
doc.Styles.Add(titleStyle1)
# 定义大纲级别2
titleStyle2 = ParagraphStyle(doc)
titleStyle2.Name = "T2S"
titleStyle2.ParagraphFormat.OutlineLevel = OutlineLevel.Level2
titleStyle2.CharacterFormat.Bold = True
titleStyle2.CharacterFormat.FontName = "微软雅黑"
titleStyle2.CharacterFormat.FontSize = 16
titleStyle2.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left
doc.Styles.Add(titleStyle2)
# 定义大纲级别3
titleStyle3 = ParagraphStyle(doc)
titleStyle3.Name = "T3S"
titleStyle3.ParagraphFormat.OutlineLevel = OutlineLevel.Level3
titleStyle3.CharacterFormat.Bold = True
titleStyle3.CharacterFormat.FontName = "微软雅黑"
titleStyle3.CharacterFormat.FontSize = 14
titleStyle3.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left
doc.Styles.Add(titleStyle3)
# 添加目录段落
TOCparagraph = section.AddParagraph()
toc = TOCparagraph.AppendTOC(1, 3)
toc.UseHeadingStyles = False
toc.UseHyperlinks = True
toc.UseTableEntryFields = False
toc.RightAlignPageNumbers = True
toc.SetTOCLevelStyle(1, titleStyle1.Name)
toc.SetTOCLevelStyle(2, titleStyle2.Name)
toc.SetTOCLevelStyle(3, titleStyle3.Name)
# 定义字符格式
characterFormat = CharacterFormat(doc)
characterFormat.FontName = "微软雅黑"
characterFormat.FontSize = 12
# 添加段落并应用大纲级别样式1
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle1.Name)
paragraph.AppendText("概述")
# 添加段落并设置文本内容
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Spire.Doc for Python 是一款专业的 Python Word 开发组件,开发人员可以使用它轻松将 Word 文档的创建、读取、编辑和转换等功能集成到自己的 Python 应用程序中。作为一款完全独立的组件,Spire.Doc for Python 的运行环境无需安装 Microsoft Word。同时兼容大部分国产操作系统,能够在中标麒麟和中科方德等国产操作系统中正常运行。")
textRange.ApplyCharacterFormat(characterFormat)
# 添加段落并应用大纲级别样式1
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle1.Name)
paragraph.AppendText("主要功能")
# 添加段落并应用大纲级别样式2
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle2.Name)
paragraph.AppendText("仅需 Spire.Doc,无需 Microsoft Office Automation")
# 添加段落并设置文本内容
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Spire.Doc for Python 是一个完全独立的 .NET Word 类库,它所运行的系统无需安装 Microsoft Office。Spire.Doc for Python 和 Microsoft Word Automation 相比,优点在于稳定性高,运行速度快和可扩展性强。")
textRange.ApplyCharacterFormat(characterFormat)
# 添加段落并应用大纲级别样式3
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle3.Name)
paragraph.AppendText("Word 版本");
# 添加段落并设置文本内容
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Word97-03 Word2007 Word2010 Word2013 Word2016 Word2019")
textRange.ApplyCharacterFormat(characterFormat)
# 添加段落并应用大纲级别样式2
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle2.Name)
paragraph.AppendText("高质量的文档转换")
# 添加段落并设置文本内容
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Spire.Doc for Python 能帮助用户将 Word 文件保存在流中,还支持将 Word 文件与 XML、RTF、EMF、TXT、XPS、EPUB、HTML、SVG 和 ODT 等格式文件之间的双向转换。同时,它还支持将 Word 文件转换为 PDF 文件和 OFD,HTML 文件转换为图像文件。")
textRange.ApplyCharacterFormat(characterFormat)
# 添加段落并应用大纲级别样式2
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle2.Name)
paragraph.AppendText("多样化的 Word 文档功能")
# 添加段落并设置文本内容
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Spire.Doc for Python 的一个常用功能就是动态创建一个全新的 Word 文档。并支持几乎所有的 Word 文档元素,它们主要包括页面、节、页眉、页脚、数字签名、脚注、尾注、段落、项目符号和编号、表格、文本、域、超链接、书签、注释、图片、样式、背景设置、打印功能、文档设置和文档保护。同时,也支持形状、文本框、图片、OLE 对象、公式和内容控件。")
textRange.ApplyCharacterFormat(characterFormat)
# 更新目录内容
doc.UpdateTableOfContents()
# 保存文档
doc.SaveToFile("利用大纲级别样式创建目录.docx", FileFormat.Docx2016)
# 释放资源
doc.Dispose()
Python 利用图片标题创建目录
使用 Spire.Doc 库,您可以利用 TableOfContent(Document, " \\h \\z \\c \"图片\"") 方法通过图片标题来创建目录。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.AddSection() 方法添加一个节。
- 创建一个目录对象 tocForImage = TableOfContent(Document, " \\h \\z \\c \"图片\"") 并指定目录的样式。
- 使用 Section.AddParagraph() 方法添加一个段落。
- 使用 Paragraph.Items.Add(tocForImage) 方法将目录对象添加到段落中 。
- 使用 Paragraph.AppendFieldMark(FieldMarkType.FieldSeparator) 方法添加字段分隔符。
- 使用 Paragraph.AppendText("TOC") 方法添加文本内容 "TOC"。
- 使用 Paragraph.AppendFieldMark(FieldMarkType.FieldEnd) 方法添加字段结束标记。
- 使用 Paragraph.AppendPicture() 方法添加一个图片。
- 使用 DocPicture.AddCaption() 方法添加图片标题段落,包括产品信息和格式设置.
- 利用 Document.UpdateTableOfContents(tocForImage) 方法更新目录以反映文档中的更改。
- 使用 Document.SaveToFile() 方法保存到文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个新文档对象
doc = Document()
# 在文档中添加一个节
section = doc.AddSection()
# 创建一个图片目录对象
tocForImage = TableOfContent(doc, " \\h \\z \\c \"图片\"")
# 在节中添加一个段落
tocParagraph = section.Body.AddParagraph()
# 将图片目录对象添加到段落中
tocParagraph.Items.Add(tocForImage)
# 添加字段分隔符
tocParagraph.AppendFieldMark(FieldMarkType.FieldSeparator)
# 添加文本内容
tocParagraph.AppendText("TOC")
# 添加字段结束标记
tocParagraph.AppendFieldMark(FieldMarkType.FieldEnd)
# 在节中添加一个空白段落
section.Body.AddParagraph()
# 在节中添加一个段落
paragraph = section.Body.AddParagraph()
# 添加一个图片
docPicture = paragraph.AppendPicture("images/DOC-Python.png")
docPicture.Width = 100
docPicture.Height = 100
# 添加图片标题段落
obj = docPicture.AddCaption("图片",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" Spire.Doc for Python 产品")
paragraph.Format.AfterSpacing = 20
# 继续在节中添加段落
paragraph = section.Body.AddParagraph()
docPicture = paragraph.AppendPicture("images/PDF-Python.png")
docPicture.Width = 100
docPicture.Height = 100
obj = docPicture.AddCaption("图片",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" Spire.PDF for Python 产品")
paragraph.Format.AfterSpacing = 20
paragraph = section.Body.AddParagraph()
docPicture = paragraph.AppendPicture("images/XLS-Python.png")
docPicture.Width = 100
docPicture.Height = 100
obj = docPicture.AddCaption("图片",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" Spire.XLS for Python 产品")
paragraph.Format.AfterSpacing = 20
paragraph = section.Body.AddParagraph()
docPicture = paragraph.AppendPicture("images/PPT-Python.png")
docPicture.Width = 100
docPicture.Height = 100
obj = docPicture.AddCaption("图片",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" Spire.Presentation for Python 产品")
paragraph.Format.AfterSpacing = 20
# 更新目录
doc.UpdateTableOfContents(tocForImage)
# 将文档保存为文件
doc.SaveToFile("利用图片题注创建目录.docx", FileFormat.Docx2016)
# 释放文档对象
doc.Dispose()
Python 利用表格标题创建目录
同样也可以通过 TableOfContent(Document, " \\h \\z \\c \"表格\"") 方法利用表格标题来创建目录。以下是详细的步骤:
- 创建一个 Document 对象。
- 使用 Document.AddSection() 方法添加一个节。
- 创建一个目录对象 tocForTable = TableOfContent(Document, " \\h \\z \\c \"表格\"") 并指定目录的样式。
- 使用 Section.AddParagraph() 方法添加一个段落。
- 使用 Paragraph.Items.Add(tocForTable) 方法将目录对象添加到段落中 。
- 使用 Paragraph.AppendFieldMark(FieldMarkType.FieldSeparator) 方法添加字段分隔符。
- 使用 Paragraph.AppendText("TOC") 方法添加文本内容 "TOC"。
- 使用 Paragraph.AppendFieldMark(FieldMarkType.FieldEnd) 方法添加字段结束标记。
- 使用 Section.AddTable() 方法添加一个表格并通过 Table.ResetCells(int rowsNum, int columnsNum) 方法设置行数和列数。
- 使用 Table.AddCaption() 方法添加表格片标题段落,包括产品信息和格式设置.
- 利用 Document.UpdateTableOfContents(tocForTable) 方法更新目录以反映文档中的更改。
- 使用 Document.SaveToFile() 方法保存到文档。
- Python
from spire.doc import *
from spire.doc.common import *
# 创建一个新文档
doc = Document()
# 在文档中添加一个节
section = doc.AddSection()
# 创建一个表格目录对象
tocForTable = TableOfContent(doc, " \\h \\z \\c \"表格\"")
# 在节中添加一个段落用于放置表格目录
tocParagraph = section.Body.AddParagraph()
tocParagraph.Items.Add(tocForTable)
tocParagraph.AppendFieldMark(FieldMarkType.FieldSeparator)
tocParagraph.AppendText("TOC")
tocParagraph.AppendFieldMark(FieldMarkType.FieldEnd)
# 在节中添加两个空白段落
section.Body.AddParagraph()
section.Body.AddParagraph()
# 在节中添加一个表格
table = section.Body.AddTable(True)
table.ResetCells(1, 3)
# 添加一个表格的标题
obj = table.AddCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" 一行三列")
paragraph.Format.AfterSpacing = 20
# 在节中添加一个新的表格
table = section.Body.AddTable(True)
table.ResetCells(3, 3)
# 添加第二个表格的标题
obj = table.AddCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" 三行三列")
paragraph.Format.AfterSpacing = 20
# 在节中添加另一个新的表格
table = section.Body.AddTable(True)
table.ResetCells(5, 3)
# 添加第三个表格的标题
obj = table.AddCaption("表格", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" 五行三列")
paragraph.Format.AfterSpacing = 20
# 更新表格目录
doc.UpdateTableOfContents(tocForTable)
# 将文档保存为指定文件
doc.SaveToFile("利用表格标题创建目录.docx", FileFormat.Docx2016)
# 释放资源
doc.Dispose()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。