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