1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
Character Style
===============
Word allows a set of run-level properties to be given a name. The set of
properties is called a *character style*. All the settings may be applied to
a run in a single action by setting the style of the run.
Protocol
--------
There are two call protocols related to character style: getting and setting
the character style of a run, and specifying a style when creating a run.
Get run style::
>>> run = p.add_run()
>>> run.style
<docx.styles.style._CharacterStyle object at 0x1053ab5d0>
>>> run.style.name
'Default Paragraph Font'
Set run style using character style name::
>>> run.style = 'Emphasis'
>>> run.style.name
'Emphasis'
Set run style using character style object::
>>> run.style = document.styles['Strong']
>>> run.style.name
'Strong'
Assigning |None| to :attr:`.Run.style` causes any applied character style to
be removed. A run without a character style inherits the default character
style of the document::
>>> run.style = None
>>> run.style.name
'Default Paragraph Font'
Specifying the style of a run on creation::
>>> run = p.add_run(style='Strong')
>>> run.style.name
'Strong'
Specimen XML
------------
.. highlight:: xml
A baseline regular run::
<w:p>
<w:r>
<w:t>This is a regular paragraph.</w:t>
</w:r>
</w:p>
Adding `Emphasis` character style::
<w:p>
<w:r>
<w:rPr>
<w:rStyle w:val="Emphasis"/>
</w:rPr>
<w:t>This paragraph appears in Emphasis character style.</w:t>
</w:r>
</w:p>
A style that appears in the Word user interface (UI) with one or more spaces
in its name, such as "Subtle Emphasis", will generally have a style ID with
those spaces removed. In this example, "Subtle Emphasis" becomes
"SubtleEmphasis"::
<w:p>
<w:r>
<w:rPr>
<w:rStyle w:val="SubtleEmphasis"/>
</w:rPr>
<w:t>a few words in Subtle Emphasis style</w:t>
</w:r>
</w:p>
Schema excerpt
--------------
.. highlight:: xml
::
<xsd:complexType name="CT_R"> <!-- flattened for readibility -->
<xsd:sequence>
<xsd:element name="rPr" type="CT_RPr" minOccurs="0"/>
<xsd:group ref="EG_RunInnerContent" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="rsidRPr" type="ST_LongHexNumber"/>
<xsd:attribute name="rsidDel" type="ST_LongHexNumber"/>
<xsd:attribute name="rsidR" type="ST_LongHexNumber"/>
</xsd:complexType>
<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_String">
<xsd:attribute name="val" type="s:ST_String" use="required"/>
</xsd:complexType>
<xsd:simpleType name="ST_String">
<xsd:restriction base="xsd:string"/>
</xsd:simpleType>
|