|
| 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 | +Example: |
| 10 | + |
| 11 | + The normal font of a document is 10 point Times Roman. From time to time, |
| 12 | + a Python class name appears in-line in the text. These short runs of |
| 13 | + Python text are to appear in 9 point Courier. A character style named "Code" |
| 14 | + is defined such that these words or phrases can be set to the distinctive |
| 15 | + font and size in a single step. |
| 16 | + |
| 17 | + Later, it is decided that 10 point Menlo should be used for inline code |
| 18 | + instead. The "Code" character style is updated to the new settings and all |
| 19 | + instances of inline code in the document immediately appear in the new |
| 20 | + font. |
| 21 | + |
| 22 | + |
| 23 | +Protocol |
| 24 | +-------- |
| 25 | + |
| 26 | +There are two call protocols related to character style: getting and setting |
| 27 | +the character style of a run, and specifying a style when creating a run. |
| 28 | + |
| 29 | +Getting and setting the style of a run:: |
| 30 | + |
| 31 | + >>> run = p.add_run() |
| 32 | + >>> run.style |
| 33 | + None |
| 34 | + >>> run.style = 'Emphasis' |
| 35 | + >>> run.style |
| 36 | + 'Emphasis' |
| 37 | + >>> run.style = None |
| 38 | + >>> run.style |
| 39 | + None |
| 40 | + |
| 41 | +Assigning |None| to ``Run.style`` causes any applied character style to be |
| 42 | +removed. A run without a character style inherits the character style of its |
| 43 | +containing paragraph. |
| 44 | + |
| 45 | +Specifying the style of a run on creation:: |
| 46 | + |
| 47 | + >>> run = p.add_run() |
| 48 | + >>> run.style |
| 49 | + None |
| 50 | + >>> run = p.add_run(style='Emphasis') |
| 51 | + >>> run.style |
| 52 | + 'Emphasis' |
| 53 | + >>> run = p.add_run('text in this run', 'Strong') |
| 54 | + >>> run.style |
| 55 | + 'Strong' |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +Specimen XML |
| 60 | +------------ |
| 61 | + |
| 62 | +.. highlight:: xml |
| 63 | + |
| 64 | +A baseline regular run:: |
| 65 | + |
| 66 | + <w:p> |
| 67 | + <w:r> |
| 68 | + <w:t>This is a regular paragraph.</w:t> |
| 69 | + </w:r> |
| 70 | + </w:p> |
| 71 | + |
| 72 | +Adding *Emphasis* character style:: |
| 73 | + |
| 74 | + <w:p> |
| 75 | + <w:r> |
| 76 | + <w:rPr> |
| 77 | + <w:rStyle w:val="Emphasis"/> |
| 78 | + </w:rPr> |
| 79 | + <w:t>This paragraph appears in Emphasis character style.</w:t> |
| 80 | + </w:r> |
| 81 | + </w:p> |
| 82 | + |
| 83 | +A style that appears in the Word user interface (UI) with one or more spaces |
| 84 | +in its name, such as "Subtle Emphasis", will generally have a style ID with |
| 85 | +those spaces removed. In this example, "Subtle Emphasis" becomes |
| 86 | +"SubtleEmphasis":: |
| 87 | + |
| 88 | + <w:p> |
| 89 | + <w:r> |
| 90 | + <w:rPr> |
| 91 | + <w:rStyle w:val="SubtleEmphasis"/> |
| 92 | + </w:rPr> |
| 93 | + <w:t>a few words in Subtle Emphasis style</w:t> |
| 94 | + </w:r> |
| 95 | + </w:p> |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +Schema excerpt |
| 100 | +-------------- |
| 101 | + |
| 102 | +.. highlight:: xml |
| 103 | + |
| 104 | +:: |
| 105 | + |
| 106 | + <xsd:complexType name="CT_R"> <!-- flattened for readibility --> |
| 107 | + <xsd:sequence> |
| 108 | + <xsd:element name="rPr" type="CT_RPr" minOccurs="0"/> |
| 109 | + <xsd:group ref="EG_RunInnerContent" minOccurs="0" maxOccurs="unbounded"/> |
| 110 | + </xsd:sequence> |
| 111 | + <xsd:attribute name="rsidRPr" type="ST_LongHexNumber"/> |
| 112 | + <xsd:attribute name="rsidDel" type="ST_LongHexNumber"/> |
| 113 | + <xsd:attribute name="rsidR" type="ST_LongHexNumber"/> |
| 114 | + </xsd:complexType> |
| 115 | + |
| 116 | + <xsd:complexType name="CT_RPr"> <!-- flattened for readibility --> |
| 117 | + <xsd:sequence> |
| 118 | + <xsd:group ref="EG_RPrBase" minOccurs="0" maxOccurs="unbounded"/> |
| 119 | + <xsd:element name="rPrChange" type="CT_RPrChange" minOccurs="0"/> |
| 120 | + </xsd:sequence> |
| 121 | + </xsd:complexType> |
| 122 | + |
| 123 | + <xsd:group name="EG_RPrBase"> |
| 124 | + <xsd:choice> |
| 125 | + <xsd:element name="rStyle" type="CT_String"/> |
| 126 | + <xsd:element name="rFonts" type="CT_Fonts"/> |
| 127 | + <xsd:element name="b" type="CT_OnOff"/> |
| 128 | + <!-- 36 others --> |
| 129 | + </xsd:choice> |
| 130 | + </xsd:group> |
| 131 | + |
| 132 | + <xsd:complexType name="CT_String"> |
| 133 | + <xsd:attribute name="val" type="s:ST_String" use="required"/> |
| 134 | + </xsd:complexType> |
| 135 | + |
| 136 | + <xsd:simpleType name="ST_String"> |
| 137 | + <xsd:restriction base="xsd:string"/> |
| 138 | + </xsd:simpleType> |
0 commit comments