run
run
¶
Run-related proxy objects for python-docx, Run in particular.
Run
¶
Run(r: CT_R, parent: ProvidesStoryPart)
Bases: StoryChild
Proxy object wrapping <w:r> 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
the style hierarchy.
Source code in src/docx/text/run.py
bold
property
writable
¶
Read/write tri-state value.
When True, causes the text of the run to appear in bold face. When False,
the text unconditionally appears non-bold. When None the bold setting for this
run is inherited from the style hierarchy.
embedded_objects
property
¶
embedded_objects: List[EmbeddedObject]
The OLE objects embedded in this run, in document order.
contains_page_break
property
¶
True when one or more rendered page-breaks occur in this run.
Note that "hard" page-breaks inserted by the author are not included. A hard page-break gives rise to a rendered page-break in the right position so if those were included that page-break would be "double-counted".
It would be very rare for multiple rendered page-breaks to occur in a single run, but it is possible.
font
property
¶
font: Font
The Font object providing access to the character formatting properties for this run, such as font name and size.
italic
property
writable
¶
Read/write tri-state value.
When True, causes the text of the run to appear in italics. When False, the
text unconditionally appears non-italic. When None the italic setting for this
run is inherited from the style hierarchy.
style
property
writable
¶
style: CharacterStyle
Read/write.
A CharacterStyle object representing the character style applied to this run.
The default character style for the document (often Default Character Font) is
returned if the run has no directly-applied character style. Setting this
property to None removes any directly-applied character style.
text
property
writable
¶
String formed by concatenating the text equivalent of each run.
Each <w:t> element adds the text characters it contains. A <w:tab/> element
adds a \t character. A <w:cr/> or <w:br> element each add a \n
character. Note that a <w:br> element can indicate a page break or column
break as well as a line break. Only line-break <w:br> elements translate to
a \n character. Others are ignored. All other content child elements, such as
<w:drawing>, are ignored.
Assigning text to this property has the reverse effect, translating each \t
character to a <w:tab/> element and each \n or \r character to a
<w:cr/> element. Any existing run content is replaced. Run formatting is
preserved.
underline
property
writable
¶
underline: bool | WD_UNDERLINE | None
The underline style for this Run.
Value is one of None, True, False, or a member of WdUnderline.
A value of None indicates the run has no directly-applied underline value and
so will inherit the underline value of its containing paragraph. Assigning
None to this property removes any directly-applied underline value.
A value of False indicates a directly-applied setting of no underline,
overriding any inherited value.
A value of True indicates single underline.
The values from WdUnderline are used to specify other outline styles such as double, wavy, and dotted.
add_break
¶
Add a break element of break_type to this run.
break_type can take the values WD_BREAK.LINE, WD_BREAK.PAGE, and
WD_BREAK.COLUMN where WD_BREAK is imported from docx.enum.text.
break_type defaults to WD_BREAK.LINE.
Source code in src/docx/text/run.py
add_picture
¶
add_picture(
image_path_or_stream: str | PathLike[str] | IO[bytes],
width: int | Length | None = None,
height: int | Length | None = None,
description: str | None = None,
title: str | None = None,
svg_fallback: str
| PathLike[str]
| IO[bytes]
| None = None,
honor_exif_orientation: bool = True,
) -> InlineShape
Return InlineShape containing image identified by image_path_or_stream.
The picture is added to the end of this run.
image_path_or_stream can be a path (a string) or a file-like object containing
a binary image.
If neither width nor height is specified, the picture appears at its native size. If only one is specified, it is used to compute a scaling factor that is then applied to the unspecified dimension, preserving the aspect ratio of the image. The native size of the picture is calculated using the dots- per-inch (dpi) value specified in the image file, defaulting to 72 dpi if no value is specified, as is often the case.
description is the picture's alternative text, which is what a screen reader
announces and what an accessibility check looks for. title is the separate,
rarely-used caption-like field Word writes alongside it. Both are omitted from
the XML when None.
svg_fallback applies only when the picture is an SVG. Word records an SVG
alongside a raster rendering of it and shows the raster one wherever the vector
source cannot be used, so passing a PNG or JPEG here is what makes the picture
appear in an older Word, in a PDF export from some tools, and anywhere else the
SVG extension is not understood. Without it the fallback refers to the SVG
itself, which Word 2016 and later render but earlier versions do not.
honor_exif_orientation applies the image's EXIF Orientation tag, which a
photo off a phone or camera almost always carries: the pixels are stored in the
sensor's native orientation and the tag says how to turn them for display. Every
image viewer, browser and word processor honours it, and a library that inserts
pictures and does not produces a visibly wrong document from a correct input
file. The rotation is written into the DrawingML (a:xfrm/@rot) rather than
into the pixels, so the image part stays byte-identical and the sha1
deduplication keeps working. Pass False for an image whose pixels are already
rotated and which carries the tag anyway — some encoders write both and there
is no reliable way to detect it.
Source code in src/docx/text/run.py
add_float_picture
¶
add_float_picture(
image_path_or_stream: str | PathLike[str] | IO[bytes],
width: int | Length | None = None,
height: int | Length | None = None,
left: Length | int = 0,
top: Length | int = 0,
wrap_type: WD_WRAP_TYPE = SQUARE,
behind_text: bool = False,
relative_from_h: WD_ANCHOR_RELATIVE_FROM_H = COLUMN,
relative_from_v: WD_ANCHOR_RELATIVE_FROM_V = PARAGRAPH,
description: str | None = None,
title: str | None = None,
svg_fallback: str
| PathLike[str]
| IO[bytes]
| None = None,
honor_exif_orientation: bool = True,
) -> FloatingShape
Return a FloatingShape for a picture that text flows around.
Where add_picture puts the image in the text flow like a character, this detaches it: the image is positioned against something on the page and text wraps around it, which is what a logo in a corner or a figure beside a paragraph needs:
from docx.shared import Cm
from docx.enum.shape import WD_ANCHOR_RELATIVE_FROM_H, WD_WRAP_TYPE
run.add_float_picture(
"logo.png",
width=Cm(3),
left=Cm(1),
top=Cm(1),
relative_from_h=WD_ANCHOR_RELATIVE_FROM_H.PAGE,
wrap_type=WD_WRAP_TYPE.SQUARE,
)
image_path_or_stream, width, height, description, title,
svg_fallback and honor_exif_orientation behave exactly as they do for
add_picture.
left and top are the offset from relative_from_h and relative_from_v,
which default to the column and the paragraph — where Word puts a picture
converted from inline to floating. Assign FloatingShape.horizontal_align
afterwards to align the shape instead of offsetting it.
wrap_type selects how text flows around the shape. behind_text puts the
shape behind the text rather than over it, which is only meaningful together
with WD_WRAP_TYPE.NONE.
The shape is anchored to this run's paragraph. A floating shape must be anchored to a paragraph in the text flow: Word positions it relative to where the anchor falls, so an anchor in a paragraph that moves takes the shape with it.
Source code in src/docx/text/run.py
add_tab
¶
add_text
¶
add_embedded_object
¶
add_embedded_object(
path_or_stream: str | PathLike[str] | IO[bytes],
*,
icon: str | PathLike[str] | IO[bytes],
prog_id: str | None = None,
width: Length | None = None,
height: Length | None = None,
) -> EmbeddedObject
Embed a file in this run as an OLE object and return it.
An embedded object is a whole file carried inside the document — a spreadsheet, a PDF, another document — shown as an icon that opens the original application on double-click:
This is a different thing from Document.add_alt_chunk, which imports content and dissolves it into the document when Word opens the file; an embedded object stays a distinct file.
icon is the image Word displays for the object and is required: Word cannot
render the embedded file itself, and an object with no visual is invisible in
the document. width and height size the visual, defaulting to the icon's own
size.
prog_id is what tells Word which application to launch —
"Excel.Sheet.12", "Word.Document.12", "AcroExch.Document". Getting
it wrong produces an object Word shows but cannot open, so it is worth passing
the right one; the default of "Package" is Word's generic "some file" entry,
which prompts the user to choose an application.
The visual is VML rather than DrawingML, so this shares nothing with add_picture beyond relating the icon image in.
Source code in src/docx/text/run.py
copy_to
¶
copy_to(
paragraph: Paragraph,
*,
before: Run | None = None,
after: Run | None = None,
missing_style: str = "copy",
) -> Run
Return a copy of this run, newly placed in paragraph.
before and after place the copy relative to an existing run; with neither it
is appended.
See Paragraph.copy_to for what is repaired on the way — relationships, drawing ids, bookmarks, and, for a copy into another document, styles.
Source code in src/docx/text/run.py
clear
¶
Return reference to this run after removing all its content.
All run formatting is preserved.
iter_inner_content
¶
iter_inner_content() -> Iterator[
str | Drawing | RenderedPageBreak
]
Generate the content-items in this run in the order they appear.
NOTE: only content-types currently supported by python-docx are generated. In
this version, that is text and rendered page-breaks. Drawing is included but
currently only provides access to its XML element (CT_Drawing) on its
._drawing attribute. Drawing attributes and methods may be expanded in
future releases.
There are a number of element-types that can appear inside a run, but most of
those (w:br, w:cr, w:noBreakHyphen, w:t, w:tab) have a clear plain-text
equivalent. Any contiguous range of such elements is generated as a single
str. Rendered page-break and drawing elements are generated individually. Any
other elements are ignored.
Source code in src/docx/text/run.py
delete
¶
Remove this run from its paragraph.
As for Paragraph.delete(), a relationship referenced only from this run is
dropped and any range marker left unmatched is removed.
mark_bookmark_range
¶
Return a Bookmark named name spanning this run through last_run.
The two runs need not be in the same paragraph; a bookmark's delimiters are siblings of the content they surround rather than a container for it, which is what lets one span paragraphs and table cells.
Source code in src/docx/text/run.py
add_footnote_reference
¶
add_footnote_reference(footnote: Footnote) -> None
Add a reference to footnote at the end of this run.
Word renders the reference as the footnote number, superscripted, and places the footnote itself at the foot of the page it falls on. A footnote no run references does not appear in the rendered document at all.
The "FootnoteReference" character style is applied to this run when it has no character style of its own, since that style is what raises the mark to a superscript. Give the run a style beforehand to prevent that, or add the reference to a run of its own to keep it off surrounding text:
footnote = document.footnotes.add_footnote("See Smith (2019).")
paragraph.add_run().add_footnote_reference(footnote)
Source code in src/docx/text/run.py
add_endnote_reference
¶
add_endnote_reference(endnote: Endnote) -> None
Add a reference to endnote at the end of this run.
The endnote counterpart of add_footnote_reference, and identical to it except that Word places the note at the end of the document or section rather than at the foot of the page:
endnote = document.endnotes.add_endnote("See Smith (2019).")
paragraph.add_run().add_endnote_reference(endnote)
The "EndnoteReference" character style is applied to this run when it has no character style of its own, since that style is what raises the mark to a superscript.
Source code in src/docx/text/run.py
mark_comment_range
¶
mark_comment_range(last_run: Run, comment_id: int) -> None
Mark the range of runs from this run to last_run (inclusive) as belonging to a comment.
comment_id identfies the comment that references this range.