字体

Word支持多种字符格式。字符格式可以应用于 样式层次结构 . 在最底层,它可以直接应用于一系列文本内容。除此之外,它还可以应用于字符、段落和表格样式。它也可以应用于抽象的编号定义。在最高级别,它可以通过主题或文档默认值来应用。

字体名称

Word允许在一次运行中为字符内容指定多个字体。这允许在一次运行中使用不同的Unicode字符范围,例如ASCII和阿拉伯语,每个字符都以为该范围指定的字体呈现。

一种字体最多可指定八种不同的字体。四个用于指定不同代码点范围的字体。这些是:

  • w:ascii -用于前128个Unicode代码点

  • w:cs -用于复杂的脚本代码点

  • w:eastAsia -用于东亚代码点

  • w:hAnsi -代表 高ANSI ,但实际上是对其他三个代码点之一未指定的所有代码点的捕获。

另外四个, w:asciiThemew:csThemew:eastAsiaThemew:hAnsiTheme 用于间接指定主题定义的字体。这样可以在文档中集中设置字体。这四个属性的优先级低于前四个,因此 w:asciiTheme 如果 w:ascii 属性也存在。

用于运行的字体名称在 w:rPr/w:rFonts 元素。有8个属性组合在一起指定要使用的字体。

协议

最初,API只支持基本字体名称,使用 name 属性。它的价值是 w:rFonts/@w:ascii 属性或 None 如果不存在。对此属性的赋值将同时设置 w:ascii 以及 w:hAnsi 属性,或在以下情况下同时删除它们: None 分配:

>>> font = document.styles['Normal'].font
>>> font.name
None
>>> font.name = 'Arial'
>>> font.name
'Arial'

布尔运行属性

打开或关闭的字符格式,如粗体、斜体和小写。其中某些属性是 切换属性 如果它们在样式层次结构中出现多次,则可能会相互抵消。有关切换属性的更多详细信息,请参见§17.7.3。它们不影响此处指定的API。

以下管路特性是布尔(三态)特性:

要素

规格

名称

<b/>

§17.3.2.1

大胆的

<bCs/>

§17.3.2.2

繁体字粗体

<caps/>

§17.3.2.5

将所有字符显示为大写字母

<cs/>

§17.3.2.7

运行时使用复杂的脚本格式

<dstrike/>

§17.3.2.9

双删除线

<emboss/>

§17.3.2.13

压花

<i/>

§17.3.2.16

斜体字

<iCs/>

§17.3.2.17

复杂字体斜体

<imprint/>

§17.3.2.18

印记

<noProof/>

§17.3.2.21

不要检查拼写或语法

<oMath/>

§17.3.2.22

Office Open XML数学

<outline/>

§17.3.2.23

显示字符轮廓

<rtl/>

§17.3.2.30

从右向左文本

<shadow/>

§17.3.2.31

阴影

<smallCaps/>

§17.3.2.33

小型股

<snapToGrid/>

§17.3.2.34

对字符间距使用文档网格设置

<specVanish/>

§17.3.2.36

段落标记总是隐藏的

<strike/>

§17.3.2.37

单删除线

<vanish/>

§17.3.2.41

隐藏文本

<webHidden/>

§17.3.2.44

Web隐藏文本

协议

在API级别,每个布尔运行属性都是读/写“三态”属性,具有可能的值 TrueFalseNone .

以下交互式会话演示查询和应用运行级别属性的协议:

>>> run = p.add_run()
>>> run.bold
None
>>> run.bold = True
>>> run.bold
True
>>> run.bold = False
>>> run.bold
False
>>> run.bold = None
>>> run.bold
None

这三个值的语义如下:

价值

意思

该属性的有效值是无条件的 on 。样式层次中的相反设置不起作用。

该属性的有效值是无条件的 off 。样式层次中的相反设置不起作用。

没有

该元素不存在。有效值继承自样式层次结构。如果样式层次结构中不存在此属性的值,则有效值为 off

切换属性

某些布尔游程属性包括 toggle properties 。切换属性的行为类似于 toggle 在样式层次结构中的某些位置。此处的切换意味着将属性设置为开具有反转先前设置的效果,而不是无条件地将属性设置为开。

此行为允许在继承样式时重写(关闭)这些属性。例如,考虑一个字符样式 emphasized 那就很大胆了。另一种风格, strong 继承自 emphasized ,但应显示为斜体而不是粗体。将粗体设置为禁用无效,因为它被中的粗体覆盖 strong (我想)。因为粗体是一个切换属性,所以在 emphasized 使其值被切换为False,从而达到所需的效果。有关切换属性的更多详细信息,请参见§17.7.3。

以下运行属性是切换属性:

要素

规格

名称

<b/>

§17.3.2.1

大胆的

<bCs/>

§17.3.2.2

繁体字粗体

<caps/>

§17.3.2.5

将所有字符显示为大写字母

<emboss/>

§17.3.2.13

压花

<i/>

§17.3.2.16

斜体字

<iCs/>

§17.3.2.17

复杂字体斜体

<imprint/>

§17.3.2.18

印记

<outline/>

§17.3.2.23

显示字符轮廓

<shadow/>

§17.3.2.31

阴影

<smallCaps/>

§17.3.2.33

小型股

<strike/>

§17.3.2.37

单删除线

<vanish/>

§17.3.2.41

隐藏文本

XML样本

<w:r>
  <w:rPr>
    <w:b/>
    <w:i/>
    <w:smallCaps/>
    <w:strike/>
    <w:sz w:val="28"/>
    <w:szCs w:val="28"/>
    <w:u w:val="single"/>
  </w:rPr>
  <w:t>bold, italic, small caps, strike, 14 pt, and underline</w:t>
</w:r>

架构摘要

运行属性可能以任何顺序出现,并且每个属性可能出现多次。不知道这是什么语义,也不知道为什么要这么做,但需要注意的是。Word在写入文件时似乎按以下顺序排列它们:

<xsd:complexType name="CT_RPr">  <!-- denormalized -->
  <xsd:sequence>
    <xsd:choice minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="rStyle"          type="CT_String"/>
      <xsd:element name="rFonts"          type="CT_Fonts"/>
      <xsd:element name="b"               type="CT_OnOff"/>
      <xsd:element name="bCs"             type="CT_OnOff"/>
      <xsd:element name="i"               type="CT_OnOff"/>
      <xsd:element name="iCs"             type="CT_OnOff"/>
      <xsd:element name="caps"            type="CT_OnOff"/>
      <xsd:element name="smallCaps"       type="CT_OnOff"/>
      <xsd:element name="strike"          type="CT_OnOff"/>
      <xsd:element name="dstrike"         type="CT_OnOff"/>
      <xsd:element name="outline"         type="CT_OnOff"/>
      <xsd:element name="shadow"          type="CT_OnOff"/>
      <xsd:element name="emboss"          type="CT_OnOff"/>
      <xsd:element name="imprint"         type="CT_OnOff"/>
      <xsd:element name="noProof"         type="CT_OnOff"/>
      <xsd:element name="snapToGrid"      type="CT_OnOff"/>
      <xsd:element name="vanish"          type="CT_OnOff"/>
      <xsd:element name="webHidden"       type="CT_OnOff"/>
      <xsd:element name="color"           type="CT_Color"/>
      <xsd:element name="spacing"         type="CT_SignedTwipsMeasure"/>
      <xsd:element name="w"               type="CT_TextScale"/>
      <xsd:element name="kern"            type="CT_HpsMeasure"/>
      <xsd:element name="position"        type="CT_SignedHpsMeasure"/>
      <xsd:element name="sz"              type="CT_HpsMeasure"/>
      <xsd:element name="szCs"            type="CT_HpsMeasure"/>
      <xsd:element name="highlight"       type="CT_Highlight"/>
      <xsd:element name="u"               type="CT_Underline"/>
      <xsd:element name="effect"          type="CT_TextEffect"/>
      <xsd:element name="bdr"             type="CT_Border"/>
      <xsd:element name="shd"             type="CT_Shd"/>
      <xsd:element name="fitText"         type="CT_FitText"/>
      <xsd:element name="vertAlign"       type="CT_VerticalAlignRun"/>
      <xsd:element name="rtl"             type="CT_OnOff"/>
      <xsd:element name="cs"              type="CT_OnOff"/>
      <xsd:element name="em"              type="CT_Em"/>
      <xsd:element name="lang"            type="CT_Language"/>
      <xsd:element name="eastAsianLayout" type="CT_EastAsianLayout"/>
      <xsd:element name="specVanish"      type="CT_OnOff"/>
      <xsd:element name="oMath"           type="CT_OnOff"/>
    </xsd:choice>
    <xsd:element name="rPrChange" type="CT_RPrChange" minOccurs="0"/>
  </xsd:sequence>
</xsd:group>

<xsd:complexType name="CT_Fonts">
  <xsd:attribute name="hint"          type="ST_Hint"/>
  <xsd:attribute name="ascii"         type="s:ST_String"/>
  <xsd:attribute name="hAnsi"         type="s:ST_String"/>
  <xsd:attribute name="eastAsia"      type="s:ST_String"/>
  <xsd:attribute name="cs"            type="s:ST_String"/>
  <xsd:attribute name="asciiTheme"    type="ST_Theme"/>
  <xsd:attribute name="hAnsiTheme"    type="ST_Theme"/>
  <xsd:attribute name="eastAsiaTheme" type="ST_Theme"/>
  <xsd:attribute name="cstheme"       type="ST_Theme"/>
</xsd:complexType>

<xsd:complexType name="CT_HpsMeasure">
  <xsd:attribute name="val" type="ST_HpsMeasure" use="required"/>
</xsd:complexType>

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

<xsd:complexType name="CT_SignedHpsMeasure">
  <xsd:attribute name="val" type="ST_SignedHpsMeasure" use="required"/>
</xsd:complexType>

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

<xsd:complexType name="CT_Underline">
  <xsd:attribute name="val"        type="ST_Underline"/>
  <xsd:attribute name="color"      type="ST_HexColor"/>
  <xsd:attribute name="themeColor" type="ST_ThemeColor"/>
  <xsd:attribute name="themeTint"  type="ST_UcharHexNumber"/>
  <xsd:attribute name="themeShade" type="ST_UcharHexNumber"/>
</xsd:complexType>

<xsd:complexType name="CT_VerticalAlignRun">
  <xsd:attribute name="val" type="s:ST_VerticalAlignRun" use="required"/>
</xsd:complexType>

<!-- simple types -->

<xsd:simpleType name="ST_Hint">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="default"/>
    <xsd:enumeration value="eastAsia"/>
    <xsd:enumeration value="cs"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_HpsMeasure">
  <xsd:union memberTypes="s:ST_UnsignedDecimalNumber
                          s:ST_PositiveUniversalMeasure"/>
</xsd:simpleType>

<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_PositiveUniversalMeasure">
  <xsd:restriction base="ST_UniversalMeasure">
    <xsd:pattern value="[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_SignedHpsMeasure">
  <xsd:union memberTypes="xsd:integer s:ST_UniversalMeasure"/>
</xsd:simpleType>

<xsd:simpleType name="ST_Theme">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="majorEastAsia"/>
    <xsd:enumeration value="majorBidi"/>
    <xsd:enumeration value="majorAscii"/>
    <xsd:enumeration value="majorHAnsi"/>
    <xsd:enumeration value="minorEastAsia"/>
    <xsd:enumeration value="minorBidi"/>
    <xsd:enumeration value="minorAscii"/>
    <xsd:enumeration value="minorHAnsi"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_Underline">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="single"/>
    <xsd:enumeration value="words"/>
    <xsd:enumeration value="double"/>
    <xsd:enumeration value="thick"/>
    <xsd:enumeration value="dotted"/>
    <xsd:enumeration value="dottedHeavy"/>
    <xsd:enumeration value="dash"/>
    <xsd:enumeration value="dashedHeavy"/>
    <xsd:enumeration value="dashLong"/>
    <xsd:enumeration value="dashLongHeavy"/>
    <xsd:enumeration value="dotDash"/>
    <xsd:enumeration value="dashDotHeavy"/>
    <xsd:enumeration value="dotDotDash"/>
    <xsd:enumeration value="dashDotDotHeavy"/>
    <xsd:enumeration value="wave"/>
    <xsd:enumeration value="wavyHeavy"/>
    <xsd:enumeration value="wavyDouble"/>
    <xsd:enumeration value="none"/>
  </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ST_UnsignedDecimalNumber">
  <xsd:restriction base="xsd:unsignedLong"/>
</xsd:simpleType>

<xsd:simpleType name="ST_VerticalAlignRun">
  <xsd:restriction base="xsd:string">
    <xsd:enumeration value="baseline"/>
    <xsd:enumeration value="superscript"/>
    <xsd:enumeration value="subscript"/>
  </xsd:restriction>
</xsd:simpleType>