1
1
"""Run-related proxy objects for python-docx, Run in particular."""
2
2
3
+ from __future__ import annotations
4
+
5
+ from typing import IO
6
+
7
+ from docx import types as t
3
8
from docx .enum .style import WD_STYLE_TYPE
4
9
from docx .enum .text import WD_BREAK
10
+ from docx .oxml .text .run import CT_R , CT_Text
5
11
from docx .shape import InlineShape
6
- from docx .shared import Parented
12
+ from docx .shared import Length , Parented
13
+ from docx .styles .style import CharacterStyle
7
14
from docx .text .font import Font
8
15
9
16
10
17
class Run (Parented ):
11
- """Proxy object wrapping `` <w:r>` ` element.
18
+ """Proxy object wrapping `<w:r>` element.
12
19
13
20
Several of the properties on Run take a tri-state value, |True|, |False|, or |None|.
14
21
|True| and |False| correspond to on and off respectively. |None| indicates the
15
22
property is not specified directly on the run and its effective value is taken from
16
23
the style hierarchy.
17
24
"""
18
25
19
- def __init__ (self , r , parent ):
26
+ def __init__ (self , r : CT_R , parent : t . StoryChild ):
20
27
super (Run , self ).__init__ (parent )
21
28
self ._r = self ._element = self .element = r
22
29
23
- def add_break (self , break_type : WD_BREAK = WD_BREAK .LINE ):
30
+ def add_break (self , break_type : WD_BREAK = WD_BREAK .LINE ): # pyright: ignore
24
31
"""Add a break element of `break_type` to this run.
25
32
26
33
`break_type` can take the values `WD_BREAK.LINE`, `WD_BREAK.PAGE`, and
@@ -41,7 +48,12 @@ def add_break(self, break_type: WD_BREAK = WD_BREAK.LINE):
41
48
if clear is not None :
42
49
br .clear = clear
43
50
44
- def add_picture (self , image_path_or_stream , width = None , height = None ):
51
+ def add_picture (
52
+ self ,
53
+ image_path_or_stream : str | IO [bytes ],
54
+ width : Length | None = None ,
55
+ height : Length | None = None ,
56
+ ) -> InlineShape :
45
57
"""Return an |InlineShape| instance containing the image identified by
46
58
`image_path_or_stream`, added to the end of this run.
47
59
@@ -62,7 +74,7 @@ def add_tab(self):
62
74
tab character."""
63
75
self ._r ._add_tab ()
64
76
65
- def add_text (self , text ):
77
+ def add_text (self , text : str ):
66
78
"""Returns a newly appended |_Text| object (corresponding to a new ``<w:t>``
67
79
child element) to the run, containing `text`.
68
80
@@ -73,15 +85,15 @@ def add_text(self, text):
73
85
return _Text (t )
74
86
75
87
@property
76
- def bold (self ):
88
+ def bold (self ) -> bool :
77
89
"""Read/write.
78
90
79
91
Causes the text of the run to appear in bold.
80
92
"""
81
93
return self .font .bold
82
94
83
95
@bold .setter
84
- def bold (self , value ):
96
+ def bold (self , value : bool ):
85
97
self .font .bold = value
86
98
87
99
def clear (self ):
@@ -99,19 +111,19 @@ def font(self):
99
111
return Font (self ._element )
100
112
101
113
@property
102
- def italic (self ):
114
+ def italic (self ) -> bool :
103
115
"""Read/write tri-state value.
104
116
105
117
When |True|, causes the text of the run to appear in italics.
106
118
"""
107
119
return self .font .italic
108
120
109
121
@italic .setter
110
- def italic (self , value ):
122
+ def italic (self , value : bool ):
111
123
self .font .italic = value
112
124
113
125
@property
114
- def style (self ):
126
+ def style (self ) -> CharacterStyle | None :
115
127
"""Read/write.
116
128
117
129
A |_CharacterStyle| object representing the character style applied to this run.
@@ -123,7 +135,7 @@ def style(self):
123
135
return self .part .get_style (style_id , WD_STYLE_TYPE .CHARACTER )
124
136
125
137
@style .setter
126
- def style (self , style_or_name ):
138
+ def style (self , style_or_name : str | CharacterStyle | None ):
127
139
style_id = self .part .get_style_id (style_or_name , WD_STYLE_TYPE .CHARACTER )
128
140
self ._r .style = style_id
129
141
@@ -146,11 +158,11 @@ def text(self) -> str:
146
158
return self ._r .text
147
159
148
160
@text .setter
149
- def text (self , text ):
161
+ def text (self , text : str ):
150
162
self ._r .text = text
151
163
152
164
@property
48D1
code>
153
- def underline (self ):
165
+ def underline (self ) -> bool :
154
166
"""The underline style for this |Run|, one of |None|, |True|, |False|, or a
155
167
value from :ref:`WdUnderline`.
156
168
@@ -165,13 +177,13 @@ def underline(self):
165
177
return self .font .underline
166
178
167
179
@underline .setter
168
- def underline (self , value ):
180
+ def underline (self , value : bool ):
169
181
self .font .underline = value
170
182
171
183
172
184
class _Text (object ):
173
185
"""Proxy object wrapping `<w:t>` element."""
174
186
175
- def __init__ (self , t_elm ):
187
+ def __init__ (self , t_elm : CT_Text ):
176
188
super (_Text , self ).__init__ ()
177
189
self ._t = t_elm
0 commit comments