Paper Office
paper-docxAPI referenceparts.image

ImagePart

paper-docx 0.2.0 API reference

An image part.

Corresponds to the target part of a relationship with type RELATIONSHIP_TYPE.IMAGE.

ImagePart(partname: PackURI, content_type: str, blob: bytes, image: Image | None = None)

Attributes

blob

attributeblobbytes

Contents of this package part as a sequence of bytes.

May be text or binary. Intended to be overridden by subclasses. Default behavior is to return load blob.

content_type

attributecontent_type

Content type of this part.

default_cx

attributedefault_cx

Native width of this image, calculated from its width in pixels and horizontal dots per inch (dpi).

default_cy

attributedefault_cy

Native height of this image, calculated from its height in pixels and vertical dots per inch (dpi).

filename

attributefilename

Filename from which this image part was originally created.

A generic name, e.g. 'image.png', is substituted if no name is available, for example when the image was loaded from an unnamed stream. In that case a default extension is applied based on the detected MIME type of the image.

image

attributeimageImage

package

attributepackage

OpcPackage instance this part belongs to.

partname

attributepartname

PackURI instance holding partname of this part, e.g. '/ppt/slides/slide1.xml'.

attributerelated_parts

Dictionary mapping related parts by rId, so child objects can resolve explicit relationships present in the part XML, e.g. sldIdLst to a specific Slide instance.

rels

attributerels

Relationships instance holding the relationships for this part.

sha1

attributesha1

SHA1 hash digest of the blob of this image part.

Functions

after_unmarshal

funcafter_unmarshal()

Entry point for post-unmarshaling processing, for example to parse the part XML.

May be overridden by subclasses without forwarding call to super.

before_marshal

funcbefore_marshal()

Entry point for pre-serialization processing, for example to finalize part naming if necessary.

May be overridden by subclasses without forwarding call to super.

drop_rel

funcdrop_rel(rId: str)

Remove the relationship identified by rId if its reference count is less than 2.

Relationships with a reference count of 0 are implicit relationships.

paramrIdstr

from_image

funcfrom_image(image: Image, partname: PackURI)

Return an ImagePart instance newly created from image and assigned partname.

paramimageImage
parampartnamePackURI

load

funcload(partname: PackURI, content_type: str, blob: bytes, package: OpcPackage)

Called by docx.opc.package.PartFactory to load an image part from a package being opened by Document(...) call.

parampartnamePackURI
paramcontent_typestr
paramblobbytes
parampackageOpcPackage

load_rel

funcload_rel(reltype: str, target: Part | str, rId: str, is_external: bool = False)

Return newly added _Relationship instance of reltype.

The new relationship relates the target part to this part with key rId.

Target mode is set to RTM.EXTERNAL if is_external is True. Intended for use during load from a serialized package, where the rId is well-known. Other methods exist for adding a new relationship to a part when manipulating a part.

paramreltypestr
paramtargetPart | str
paramrIdstr
paramis_externalbool
= False
funcpart_related_by(reltype: str) -> Part

Return part to which this part has a relationship of reltype.

Raises KeyError if no such relationship is found and ValueError if more than one such relationship is found. Provides ability to resolve implicitly related part, such as Slide -> SlideLayout.

paramreltypestr

Returns

docx.opc.part.Part

relate_to

funcrelate_to(target: Part | str, reltype: str, is_external: bool = False) -> str

Return rId key of relationship of reltype to target.

The returned rId is from an existing relationship if there is one, otherwise a new relationship is created.

paramtargetPart | str
paramreltypestr
paramis_externalbool
= False

Returns

str

target_ref

functarget_ref(rId: str) -> str

Return URL contained in target ref of relationship identified by rId.

paramrIdstr

Returns

str

On this page