设置对象样式

样式是四种类型之一:字符、段落、表格或编号。所有样式对象都有行为属性和格式属性。格式属性集因样式类型而异。一般来说,格式属性是沿着这个层次结构继承的:character->paragraph->table。编号样式没有格式特性,也不继承。

行为特性

有六种行为属性:

隐藏的

Style用于分配格式属性,但在任何情况下都不会出现在用户界面中。用于 internal 由不应受最终用户控制的应用程序分配的样式。

优先

确定UI呈现的序列中样式的排序顺序。

半隐藏的

样式隐藏在所谓的“主”用户界面中。这个词的意思是 推荐名单 还有样式库。样式仍显示在 所有样式 名单。

unhide_when_used

标记应用程序,以便在下次使用样式时设置半隐藏False。

quick_style

在样式库中显示未隐藏的样式。

锁定的

当文档格式保护处于活动状态时,样式被隐藏,无法应用。

隐藏的

这个 hidden 属性在内置样式上不起作用,它在自定义样式上的行为不稳定。暂时跳过此属性。会重新考虑是否有人要求它,并可以提供一个具体的用例。

行为

范围。 hidden 在“正常”或“标题1”样式下根本不起作用。它对称呼也不起作用。没有 w:defHidden 属性对 w:latentStyles ,为假设它不支持内置样式提供了可信度。 假设: 不适用于内置样式。

UI行为。 具有 w:hidden 设置 True 在“库”和“所有样式”窗格列表中隐藏。但是,当光标位于样式窗格的“所选文本的当前样式”框中时,它会出现在该样式的段落上。用户可以从此当前样式UI元素修改样式。用户可以为具有隐藏样式的段落指定新样式。

优先

这个 priority 属性是确定样式在UI列表中的位置的整数主排序键。二级排序按名称的字母顺序排列。负值是有效的,尽管不是由Word本身赋值的,并且似乎被视为0。

行为

违约。 对于自定义样式,Word behavior似乎默认优先级为0。规范表明有效的默认值在概念上是无限的,因此样式出现在样式列表的末尾,可能是在没有指定优先级的其他样式中按字母顺序排列的。

候选方案

>>> style = document.styles['Foobar']
>>> style.priority
None
>>> style.priority = 7
>>> style.priority
7
>>> style.priority = -42
>>> style.priority
0

半隐藏的

这个 w:semiHidden 元素指定样式在所谓的 main 用户界面。对于Word,这意味着样式库以及推荐的、使用中的样式和当前文档中的列表。然后,样式窗格中的所有样式列表和当前样式下拉列表将被视为 advanced 用户界面。

行为

违约。 如果 w:semiHidden 元素被省略,其有效值为 False . 此值没有继承。

范围。 同时适用于内置样式和自定义样式。

言语行为。 Word不使用 @w:val 属性。它写着 <w:semiHidden/> 对于 True 并且省略了 False .

候选方案

>>> style = document.styles['Foo']
>>> style.hidden
False
>>> style.hidden = True
>>> style.hidden
True

示例XML

样式.隐藏=正确:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:semiHidden/>
</w:style>

样式.隐藏=错误:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
</w:style>

替代结构还应报告正确的值,但在编写XML时不能使用:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:semiHidden w:val="0"/>  <!-- style.hidden is False -->
</w:style>

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:semiHidden w:val="1"/>  <!-- style.hidden is True -->
</w:style>

使用时取消隐藏

这个 w:unhideWhenUsed 元素向应用程序发出信号,表示下次使用此样式时应使其可见。

行为

违约。 如果 w:unhideWhenUsed 元素被省略,其有效值为 False . 此值没有继承。

言语行为。 这个 w:unhideWhenUsed 下次使用样式时不会更改或删除元素。只有 w:semiHidden 元素受到影响(如果存在)。大概是这样一个样式可以被重新隐藏,以便在以后的使用中不被隐藏。

注意,Word中的这种行为只由用户实际应用样式时触发。仅仅加载在其内容中某处应用样式的文档不会导致 w:semiHidden 要删除的元素。

候选方案

>>> style = document.styles['Foo']
>>> style.unhide_when_used
False
>>> style.unhide_when_used = True
>>> style.unhide_when_used
True

示例XML

样式。使用时取消隐藏=正确:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:semiHidden/>
  <w:unhideWhenUsed/>
</w:style>

样式。使用时取消隐藏=错误:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
</w:style>

替代结构还应报告正确的值,但在编写XML时不能使用:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:unhideWhenUsed w:val="0"/>  <!-- style.unhide_when_used is False -->
</w:style>

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:unhideWhenUsed w:val="1"/>  <!-- style.unhide_when_used is True -->
</w:style>

快速风格

这个 w:qFormat 元素指定Word是否应在样式库中显示此样式。要在库中显示,此属性必须是 Truehidden 必须是 False .

行为

违约。 如果 w:qFormat 元素被省略,其有效值为 False . 此值没有继承。

言语行为。 如果 w:qFormatTrue 并且样式不会隐藏,它将按指定的顺序显示在库中 w:uiPriority .

候选方案

>>> style = document.styles['Foo']
>>> style.quick_style
False
>>> style.quick_style = True
>>> style.quick_style
True

示例XML

style.quicku风格=正确:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:qFormat/>
</w:style>

style.quicku风格=错误:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
</w:style>

替代结构还应报告正确的值,但在编写XML时不能使用:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:qFormat w:val="0"/>  <!-- style.quick_style is False -->
</w:style>

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:qFormat w:val="1"/>  <!-- style.quick_style is True -->
</w:style>

锁定的

这个 w:locked 元素指定Word是否应阻止此样式应用于内容。此行为仅在启用格式保护时处于活动状态。

行为

违约。 如果 w:locked 元素被省略,其有效值为 False . 此值没有继承。

候选方案

>>> style = document.styles['Foo']
>>> style.locked
False
>>> style.locked = True
>>> style.locked
True

示例XML

style.锁定=正确:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:locked/>
</w:style>

style.锁定=错误:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
</w:style>

替代结构还应报告正确的值,但在编写XML时不能使用:

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:locked w:val="0"/>  <!-- style.locked is False -->
</w:style>

<w:style w:type="paragraph" w:styleId="Foo">
  <w:name w:val="Foo"/>
  <w:locked w:val="1"/>  <!-- style.locked is True -->
</w:style>

候选协议

识别::

>>> style = document.styles['Body Text']
>>> style.name
'Body Text'
>>> style.style_id
'BodyText'
>>> style.type
WD_STYLE_TYPE.PARAGRAPH (1)

delete() ::

>>> len(styles)
6
>>> style.delete()
>>> len(styles)
5
>>> styles['Citation']
KeyError: no style with id or name 'Citation'

Style.base_风格:

>>> style = styles.add_style('Citation', WD_STYLE_TYPE.PARAGRAPH)
>>> style.base_style
None
>>> style.base_style = styles['Normal']
>>> style.base_style
<docx.styles.style._ParagraphStyle object at 0x10a7a9550>
>>> style.base_style.name
'Normal'

示例XML

<w:styles>

  <!-- ... -->

  <w:style w:type="paragraph" w:default="1" w:styleId="Normal">
    <w:name w:val="Normal"/>
    <w:qFormat/>
  </w:style>
  <w:style w:type="character" w:default="1" w:styleId="DefaultParagraphFont">
    <w:name w:val="Default Paragraph Font"/>
    <w:uiPriority w:val="1"/>
    <w:semiHidden/>
    <w:unhideWhenUsed/>
  </w:style>
  <w:style w:type="table" w:default="1" w:styleId="TableNormal">
    <w:name w:val="Normal Table"/>
    <w:uiPriority w:val="99"/>
    <w:semiHidden/>
    <w:unhideWhenUsed/>
    <w:tblPr>
      <w:tblInd w:w="0" w:type="dxa"/>
      <w:tblCellMar>
        <w:top w:w="0" w:type="dxa"/>
        <w:left w:w="108" w:type="dxa"/>
        <w:bottom w:w="0" w:type="dxa"/>
        <w:right w:w="108" w:type="dxa"/>
      </w:tblCellMar>
    </w:tblPr>
  </w:style>
  <w:style w:type="numbering" w:default="1" w:styleId="NoList">
    <w:name w:val="No List"/>
    <w:uiPriority w:val="99"/>
    <w:semiHidden/>
    <w:unhideWhenUsed/>
  </w:style>

  <w:style w:type="paragraph" w:customStyle="1" w:styleId="Foobar">
    <w:name w:val="Foobar"/>
    <w:basedOn w:val="Normal"/>
    <w:qFormat/>
  </w:style>

</w:styles>

架构摘要

<xsd:complexType name="CT_Style">
  <xsd:sequence>
    <xsd:element name="name"            type="CT_String"        minOccurs="0"/>
    <xsd:element name="aliases"         type="CT_String"        minOccurs="0"/>
    <xsd:element name="basedOn"         type="CT_String"        minOccurs="0"/>
    <xsd:element name="next"            type="CT_String"        minOccurs="0"/>
    <xsd:element name="link"            type="CT_String"        minOccurs="0"/>
    <xsd:element name="autoRedefine"    type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="hidden"          type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="uiPriority"      type="CT_DecimalNumber" minOccurs="0"/>
    <xsd:element name="semiHidden"      type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="unhideWhenUsed"  type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="qFormat"         type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="locked"          type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="personal"        type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="personalCompose" type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="personalReply"   type="CT_OnOff"         minOccurs="0"/>
    <xsd:element name="rsid"            type="CT_LongHexNumber" minOccurs="0"/>
    <xsd:element name="pPr"             type="CT_PPrGeneral"    minOccurs="0"/>
    <xsd:element name="rPr"             type="CT_RPr"           minOccurs="0"/>
    <xsd:element name="tblPr"           type="CT_TblPrBase"     minOccurs="0"/>
    <xsd:element name="trPr"            type="CT_TrPr"          minOccurs="0"/>
    <xsd:element name="tcPr"            type="CT_TcPr"          minOccurs="0"/>
    <xsd:element name="tblStylePr"      type="CT_TblStylePr"    minOccurs="0" maxOccurs="unbounded"/>
  </xsd:sequence>
  <xsd:attribute name="type"        type="ST_StyleType"/>
  <xsd:attribute name="styleId"     type="s:ST_String"/>
  <xsd:attribute name="default"     type="s:ST_OnOff"/>
  <xsd:attribute name="customStyle" type="s:ST_OnOff"/>
</xsd:complexType>

<xsd:complexType name="CT_OnOff">
  <xsd:attribute name="val" type="s:ST_OnOff"/>
</xsd:complexType>

<xsd:complexType name="CT_String">
  <xsd:attribute name="val" type="s:ST_String" use="required"/>
</xsd:complexType>

<xsd:simpleType name="ST_OnOff">
  <xsd:union memberTypes="xsd:boolean ST_OnOff1"/>
</xsd:simpleType>

<xsd:simpleType name="ST_OnOff1">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="on"/>
    <xsd:enumeration value="off"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_StyleType">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="paragraph"/>
    <xsd:enumeration value="character"/>
    <xsd:enumeration value="table"/>
    <xsd:enumeration value="numbering"/>
  </xsd:restriction>
</xsd:simpleType>