python-docx官方文档翻译--用户指南08--了解样式

了解样式

蚱蜢:
“主人,为什么我的段落没有显示为我指定的样式?” 
主:
“您来到正确的页面, 蚱蜢;请继续阅读……”
Word 中的样式是什么?

相同元素的格式一致时,文档之间的沟通会更好。为了实现这种一致性,专业文档设计人员开发了一个样式表,该样式表定义了文档元素类型并指定了各个元素的格式。

例如,正文段落可能设置为 9 Pt(磅) Times Roman,行高为 11 Pt(磅),左对齐。将这些规范应用于文档的每个元素时,将获得一致且优美的外观。

Word中的样式就是可以同时应用于文档元素的一组规范。 Word具有段落样式,字符样式,表格样式和编号定义。这些分别应用于段落,文本,表格和列表。

经验丰富的程序员会将样式视为间接的级别。这样的好处是一次定义,多次应用。这样可以省去定义同一对象的工作。同时,我们可以更改定义并将定义应用到所有位置/

为什么我应用的样式没有显示?

在我可以添加一些更高级的功能来解决该问题之前,这个问题可能会出现很多,所以在这里是最重要的。

  1. 使用Word时,您可以将所有这些样式应用于事物,看起来不错的样式看起来更好,因为您不必自己制作。大多数看起来都不会比内置样式差多少。
  2. 尽管这些样式显示在用户界面中,但实际上并没有出现在您正在创建的文档中,至少直到您第一次使用它时才如此。那是一件好事。文档的样式很多,会占用很多空间。如果文件包含不常使用的所有样式定义,则会有点肿。
  3. 如果您使用文件中未定义的 python-docx 样式(如果好奇,可以在查看 styles.xml ),Word 只会忽略它,并没有改变相关元素的样式。
  4. 使用样式时,Word 会将其添加到文件中。一旦使用一次,该样式就会被文档保留下来。我想有一种摆脱它的方法,但是您必须努力解决。如果应用样式,请删除应用样式的内容,然后保存文档。样式定义保留在保存的文件中。

总结如下:
如果要在使用python-docx创建的文档中使用样式,则从其开始的文档必须包含样式定义。否则,样式无法显示。不会引发异常,只会起不作用。如果使用 “默认” 模板文档,则它包含下面列出的样式,如果不是自己设计,则大多数样式都是可能需要的。

如果您使用自己的初始文档,则需要在其中至少使用一次每种样式。您不必保留内容,但是在保存文档之前,您至少使用对应样式一次。创建一个单字的段落,依次应用五种样式,然后删除该段落即可。这就是我将以下内容放入默认模板的方法:)。

Glossary

样式定义
  文档的 styles part 中的 <w:style> 元素,用于显式定义样式的属性。

定义样式
  在文档中明确定义的样式。与潜在风格形成鲜明对比。

内置样式
  Word 内置的 276 种预设样式中的一种,例如“标题1”。内置样式可以是定义的或潜在的。尚未定义的内置样式称为潜在样式。在 Word 的样式面板和样式库中,已定义和潜在的内置样式都可能显示为选项。

自定义样式
   也称为用户定义的样式,它是 Word 文档中定义的非内置样式。请注意,自定义样式不能是潜在样式。

潜在样式
  在特定文档中没有定义的内置样式在该文档中称为潜在样式。潜在样式可以作为选项出现在 Word UI 中,具体取决于文档的 LatentStyles 对象中的设置。

推荐样式列表
  从 “列表:” 下拉框中选择 “推荐” 时,样式列表显示在样式工具箱或面板中。

样式库
  在 Word UI 的功能区中显示的示例样式的选择,可以通过单击其中的一种来应用。

识别样式

样式具有三个标识属性,即 name(名称),style_id(样式 id) 和 type (类型)。

每种样式的 name 属性都是其稳定的唯一标识符,以供访问。

样式的 style_id 在内部用于将内容对象(例如段落)设置为其样式。但是,此值是由Word自动生成的,不能保证在保存过程中保持稳定。通常,仅通过从英文样式名称中删除空格来形成样式id,但是也有例外。 python-docx 的用户通常应该避免使用样式 ID,除非他们对所涉及的内部结构有信心。

样式的类型是在创建时设置的,无法更改。

样式特性

除了一组格式设置属性外,样式还具有五个用于指定其特性的属性。此特性相对简单,基本上等于样式在 Word 或 LibreOffice UI 中出现的时间和位置。

推荐样式表是理解样式特性的关键概念。在 Word 的样式窗格中,用户可以选择要查看的样式列表。其中之一被称为“推荐”,被称为“推荐列表”。这五个特性属性都会影响此列表和样式库中样式外观的某些方面。

简而言之,如果样式的 hidden 属性为 False(默认值),则该样式会出现在推荐列表中。如果未隐藏样式并且其 quick_style 属性为True,则它也会显示在样式库中。如果隐藏样式的 unhide_when_used 属性为True,则其首次使用时其 hidden 属性设置为 False。样式列表和样式库中的样式按 priority(优先级)排序,然后按字母顺序排列相同优先级的样式。如果样式的 locked 属性为 True,并且为文档设置了格式限制,则该样式将不会出现在任何列表或样式库中,并且无法应用于内容。

潜在样式

需要指定在 styles.xml 中未定义的内置样式的 UI 行为,因此需要潜在的样式定义。潜在样式定义基本上是 stub 样式定义,除了样式名称外,它最多还具有五个特性属性。通过为每个特性属性定义默认值,可以节省更多空间,因此仅需要定义与默认值不同的那些值,并且与所有默认值匹配的样式都不需要潜在的样式定义。

使用出现在 styles.xml 中的 w:latentStyles 和 w:lsdException 元素来指定潜在样式定义。

潜在样式定义仅对于内置样式是必需的,因为只有内置样式可以出现在 UI 中,而 styles.xml 中没有样式定义。

默认模板中的段落样式
  • Normal
  • Body Text
  • Body Text 2
  • Body Text 3
  • Caption
  • Heading 1
  • Heading 2
  • Heading 3
  • Heading 4
  • Heading 5
  • Heading 6
  • Heading 7
  • Heading 8
  • Heading 9
  • Intense Quote
  • List
  • List 2
  • List 3
  • List Bullet
  • List Bullet 2
  • List Bullet 3
  • List Continue
  • List Continue 2
  • List Continue 3
  • List Number
  • List Number 2
  • List Number 3
  • List Paragraph
  • Macro Text
  • No Spacing
  • Quote
  • Subtitle
  • TOCHeading
  • Title

####### 默认模板中的字符样式

  • Body Text Char
  • Body Text 2 Char
  • Body Text 3 Char
  • Book Title
  • Default Paragraph Font
  • Emphasis
  • Heading 1 Char
  • Heading 2 Char
  • Heading 3 Char
  • Heading 4 Char
  • Heading 5 Char
  • Heading 6 Char
  • Heading 7 Char
  • Heading 8 Char
  • Heading 9 Char
  • Intense Emphasis
  • Intense Quote Char
  • Intense Reference
  • Macro Text Char
  • Quote Char
  • Strong
  • Subtitle Char
  • Subtle Emphasi- s
  • Subtle Reference
  • Title Char
默认模板中的表格样式
  • Table Normal
  • Colorful Grid
  • Colorful Grid Accent 1
  • Colorful Grid Accent 2
  • Colorful Grid Accent 3
  • Colorful Grid Accent 4
  • Colorful Grid Accent 5
  • Colorful Grid Accent 6
  • Colorful List
  • Colorful List Accent 1
  • Colorful List Accent 2
  • Colorful List Accent 3
  • Colorful List Accent 4
  • Colorful List Accent 5
  • Colorful List Accent 6
  • Colorful Shading
  • Colorful Shading Accent 1
  • Colorful Shading Accent 2
  • Colorful Shading Accent 3
  • Colorful Shading Accent 4
  • Colorful Shading Accent 5
  • Colorful Shading Accent 6
  • Dark List
  • Dark List Accent 1
  • Dark List Accent 2
  • Dark List Accent 3
  • Dark List Accent 4
  • Dark List Accent 5
  • Dark List Accent 6
  • Light Grid
  • Light Grid Accent 1
  • Light Grid Accent 2
  • Light Grid Accent 3
  • Light Grid Accent 4
  • Light Grid Accent 5
  • Light Grid Accent 6
  • Light List
  • Light List Accent 1
  • Light List Accent 2
  • Light List Accent 3
  • Light List Accent 4
  • Light List Accent 5
  • Light List Accent 6
  • Light Shading
  • Light Shading Accent 1
  • Light Shading Accent 2
  • Light Shading Accent 3
  • Light Shading Accent 4
  • Light Shading Accent 5
  • Light Shading Accent 6
  • Medium Grid 1
  • Medium Grid 1 Accent 1
  • Medium Grid 1 Accent 2
  • Medium Grid 1 Accent 3
  • Medium Grid 1 Accent 4
  • Medium Grid 1 Accent 5
  • Medium Grid 1 Accent 6
  • Medium Grid 2
  • Medium Grid 2 Accent 1
  • Medium Grid 2 Accent 2
  • Medium Grid 2 Accent 3
  • Medium Grid 2 Accent 4
  • Medium Grid 2 Accent 5
  • Medium Grid 2 Accent 6
  • Medium Grid 3
  • Medium Grid 3 Accent 1
  • Medium Grid 3 Accent 2
  • Medium Grid 3 Accent 3
  • Medium Grid 3 Accent 4
  • Medium Grid 3 Accent 5
  • Medium Grid 3 Accent 6
  • Medium List 1
  • Medium List 1 Accent 1
  • Medium List 1 Accent 2
  • Medium List 1 Accent 3
  • Medium List 1 Accent 4
  • Medium List 1 Accent 5
  • Medium List 1 Accent 6
  • Medium List 2
  • Medium List 2 Accent 1
  • Medium List 2 Accent 2
  • Medium List 2 Accent 3
  • Medium List 2 Accent 4
  • Medium List 2 Accent 5
  • Medium List 2 Accent 6
  • Medium Shading 1
  • Medium Shading 1 Accent 1
  • Medium Shading 1 Accent 2
  • Medium Shading 1 Accent 3
  • Medium Shading 1 Accent 4
  • Medium Shading 1 Accent 5
  • Medium Shading 1 Accent 6
  • Medium Shading 2
  • Medium Shading 2 Accent 1
  • Medium Shading 2 Accent 2
  • Medium Shading 2 Accent 3
  • Medium Shading 2 Accent 4
  • Medium Shading 2 Accent 5
  • Medium Shading 2 Accent 6
  • Table Grid
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值