MCPcopy Index your code
hub / github.com/python-openxml/python-docx / Run

Class Run

src/docx/text/run.py:25–249  ·  view source on GitHub ↗

Proxy object wrapping ` ` element. Several of the properties on Run take a tri-state value, |True|, |False|, or |None|. |True| and |False| correspond to on and off respectively. |None| indicates the property is not specified directly on the run and its effective value is taken from

Source from the content-addressed store, hash-verified

23
24
25class Run(StoryChild):
26 """Proxy object wrapping `<w:r>` element.
27
28 Several of the properties on Run take a tri-state value, |True|, |False|, or |None|.
29 |True| and |False| correspond to on and off respectively. |None| indicates the
30 property is not specified directly on the run and its effective value is taken from
31 the style hierarchy.
32 """
33
34 def __init__(self, r: CT_R, parent: t.ProvidesStoryPart):
35 super().__init__(parent)
36 self._r = self._element = self.element = r
37
38 def add_break(self, break_type: WD_BREAK = WD_BREAK.LINE):
39 """Add a break element of `break_type` to this run.
40
41 `break_type` can take the values `WD_BREAK.LINE`, `WD_BREAK.PAGE`, and
42 `WD_BREAK.COLUMN` where `WD_BREAK` is imported from `docx.enum.text`.
43 `break_type` defaults to `WD_BREAK.LINE`.
44 """
45 type_, clear = {
46 WD_BREAK.LINE: (None, None),
47 WD_BREAK.PAGE: ("page", None),
48 WD_BREAK.COLUMN: ("column", None),
49 WD_BREAK.LINE_CLEAR_LEFT: ("textWrapping", "left"),
50 WD_BREAK.LINE_CLEAR_RIGHT: ("textWrapping", "right"),
51 WD_BREAK.LINE_CLEAR_ALL: ("textWrapping", "all"),
52 }[break_type]
53 br = self._r.add_br()
54 if type_ is not None:
55 br.type = type_
56 if clear is not None:
57 br.clear = clear
58
59 def add_picture(
60 self,
61 image_path_or_stream: str | IO[bytes],
62 width: int | Length | None = None,
63 height: int | Length | None = None,
64 ) -> InlineShape:
65 """Return |InlineShape| containing image identified by `image_path_or_stream`.
66
67 The picture is added to the end of this run.
68
69 `image_path_or_stream` can be a path (a string) or a file-like object containing
70 a binary image.
71
72 If neither width nor height is specified, the picture appears at
73 its native size. If only one is specified, it is used to compute a scaling
74 factor that is then applied to the unspecified dimension, preserving the aspect
75 ratio of the image. The native size of the picture is calculated using the dots-
76 per-inch (dpi) value specified in the image file, defaulting to 72 dpi if no
77 value is specified, as is often the case.
78 """
79 inline = self.part.new_pic_inline(image_path_or_stream, width, height)
80 self._r.add_drawing(inline)
81 return InlineShape(inline)
82

Calls

no outgoing calls