Skip to content

packuri

packuri

Provides the PackURI value type.

Also some useful known pack URI strings such as PACKAGE_URI.

PackURI

Bases: str

Provides access to pack URI components such as the baseURI and the filename slice.

Behaves as str otherwise.

baseURI property

baseURI: str

The base URI of this pack URI, the directory portion, roughly speaking.

E.g. '/ppt/slides' for '/ppt/slides/slide1.xml'. For the package pseudo- partname '/', baseURI is '/'.

ext property

ext: str

The extension portion of this pack URI, e.g. 'xml' for '/word/document.xml'.

Note the period is not included.

filename property

filename

The "filename" portion of this pack URI, e.g. 'slide1.xml' for '/ppt/slides/slide1.xml'.

For the package pseudo-partname '/', filename is ''.

idx property

idx

Return partname index as integer for tuple partname or None for singleton partname, e.g. 21 for '/ppt/slides/slide21.xml' and None for '/ppt/presentation.xml'.

membername property

membername

The pack URI with the leading slash stripped off, the form used as the Zip file membername for the package item.

Returns '' for the package pseudo-partname '/'.

rels_uri property

rels_uri

The pack URI of the .rels part corresponding to the current pack URI.

Only produces sensible output if the pack URI is a partname or the package pseudo-partname '/'.

from_rel_ref staticmethod

from_rel_ref(baseURI: str, relative_ref: str) -> PackURI

The absolute PackURI formed by translating relative_ref onto baseURI.

Source code in src/docx/opc/packuri.py
@staticmethod
def from_rel_ref(baseURI: str, relative_ref: str) -> PackURI:
    """The absolute PackURI formed by translating `relative_ref` onto `baseURI`."""
    joined_uri = posixpath.join(baseURI, relative_ref)
    abs_uri = posixpath.abspath(joined_uri)
    return PackURI(abs_uri)

relative_ref

relative_ref(baseURI: str)

Return string containing relative reference to package item from baseURI.

E.g. PackURI('/ppt/slideLayouts/slideLayout1.xml') would return '../slideLayouts/slideLayout1.xml' for baseURI '/ppt/slides'.

Source code in src/docx/opc/packuri.py
def relative_ref(self, baseURI: str):
    """Return string containing relative reference to package item from `baseURI`.

    E.g. PackURI('/ppt/slideLayouts/slideLayout1.xml') would return
    '../slideLayouts/slideLayout1.xml' for baseURI '/ppt/slides'.
    """
    # workaround for posixpath bug in 2.6, doesn't generate correct
    # relative path when `start` (second) parameter is root ('/')
    return self[1:] if baseURI == "/" else posixpath.relpath(self, baseURI)