shared
shared
¶
Objects shared by docx modules.
Length
¶
Bases: int
Base class for length constructor classes Inches, Cm, Mm, Px, and Emu.
Behaves as an int count of English Metric Units, 914,400 to the inch, 36,000 to the mm. Provides convenience unit conversion methods in the form of read-only properties. Immutable.
Emu
¶
Twips
¶
Bases: Length
Convenience constructor for length in twips, e.g. width = Twips(42).
A twip is a twentieth of a point, 635 EMU.
Pct
¶
Bases: float
A percentage, e.g. Pct(100) is one hundred percent.
A percentage is deliberately not a Length. Every unit on Length is absolute and reducible to EMU; a percentage is relative to something else and has no size of its own, so the two do not belong to the same family and mixing them silently produces nonsense.
Word stores these in fiftieths of a percent, which is what .fiftieths returns
and what from_fiftieths() reads.
RGBColor
¶
Bases: Tuple[int, int, int]
Immutable value object defining a particular RGB color.
from_string
classmethod
¶
from_string(rgb_hex_str: str) -> RGBColor
Return a new instance from an RGB color hex string like '3C2F80'.
A leading "#" is accepted, so CSS-style values like '#3C2F80' also work.
Source code in src/docx/shared.py
lazyproperty
¶
Bases: Generic[T]
Decorator like @property, but evaluated only on first access.
Like @property, this can only be used to decorate methods having only a self
parameter, and is accessed like an attribute on an instance, i.e. trailing
parentheses are not used. Unlike @property, the decorated method is only evaluated
on first access; the resulting value is cached and that same value returned on
second and later access without re-evaluation of the method.
Like @property, this class produces a data descriptor object, which is stored in the dict of the class under the name of the decorated method ('fget' nominally). The cached value is stored in the dict of the instance under that same name.
Because it is a data descriptor (as opposed to a non-data descriptor), its
__get__() method is executed on each access of the decorated attribute; the
dict item of the same name is "shadowed" by the descriptor.
While this may represent a performance improvement over a property, its greater benefit may be its other characteristics. One common use is to construct collaborator objects, removing that "real work" from the constructor, while still only executing once. It also de-couples client code from any sequencing considerations; if it's accessed from more than one location, it's assured it will be ready whenever needed.
Loosely based on: https://stackoverflow.com/a/6849299/1902513.
A lazyproperty is read-only. There is no counterpart to the optional "setter" (or deleter) behavior of an @property. This is critically important to maintaining its immutability and idempotence guarantees. Attempting to assign to a lazyproperty raises AttributeError unconditionally.
The parameter names in the methods below correspond to this usage example:
Not suitable for wrapping a function (as opposed to a method) because it is not callable.
fget is the decorated method (a "getter" function).
A lazyproperty is read-only, so there is only an fget function (a regular
@property can also have an fset and fdel function). This name was chosen for
consistency with Python's property class which uses this name for the
corresponding parameter.
Source code in src/docx/shared.py
ElementProxy
¶
ElementProxy(
element: BaseOxmlElement,
parent: ProvidesXmlPart | None = None,
)
Base class for lxml element proxy classes.
An element proxy class is one whose primary responsibilities are fulfilled by manipulating the attributes and child elements of an XML element. They are the most common type of class in python-docx other than custom element (oxml) classes.
Source code in src/docx/shared.py
Parented
¶
Parented(parent: ProvidesXmlPart)
Provides common services for document elements that occur below a part but may occasionally require an ancestor object to provide a service, such as add or drop a relationship.
Provides self._parent attribute to subclasses.
Source code in src/docx/shared.py
StoryChild
¶
StoryChild(parent: ProvidesStoryPart)
A document element within a story part.
Story parts include DocumentPart and Header/FooterPart and can contain block items (paragraphs and tables). Items from the block-item subtree occasionally require an ancestor object to provide access to part-level or package-level items like styles or images or to add or drop a relationship.
Provides self._parent attribute to subclasses.
Source code in src/docx/shared.py
TextAccumulator
¶
Accepts str fragments and joins them together, in order, on `.pop().
Handy when text in a stream is broken up arbitrarily and you want to join it back
together within certain bounds. The optional separator argument determines how
the text fragments are punctuated, defaulting to the empty string.
Source code in src/docx/shared.py
push
¶
pop
¶
Generate sero-or-one str from those accumulated.
Using yield from accum.pop() in a generator setting avoids producing an empty
string when no text is in the accumulator.
Source code in src/docx/shared.py
write_only_property
¶
@write_only_property decorator.
Creates a property (descriptor attribute) that accepts assignment, but not getattr (use in an expression).