Skip to content

pkgreader

pkgreader

Low-level, read-only API to a serialized Open Packaging Convention (OPC) package.

PackageReader

PackageReader(content_types, pkg_srels, sparts)

Provides access to the contents of a zip-format OPC package via its serialized_parts and pkg_srels attributes.

Source code in src/docx/opc/pkgreader.py
def __init__(self, content_types, pkg_srels, sparts):
    super(PackageReader, self).__init__()
    self._pkg_srels = pkg_srels
    self._sparts = sparts

from_file staticmethod

from_file(pkg_file)

Return a PackageReader instance loaded with contents of pkg_file.

Source code in src/docx/opc/pkgreader.py
@staticmethod
def from_file(pkg_file):
    """Return a |PackageReader| instance loaded with contents of `pkg_file`."""
    phys_reader = PhysPkgReader(pkg_file)
    content_types = _ContentTypeMap.from_xml(phys_reader.content_types_xml)
    pkg_srels = PackageReader._srels_for(phys_reader, PACKAGE_URI)
    sparts = PackageReader._load_serialized_parts(phys_reader, pkg_srels, content_types)
    phys_reader.close()
    return PackageReader(content_types, pkg_srels, sparts)

iter_sparts

iter_sparts()

Generate a 4-tuple (partname, content_type, reltype, blob) for each of the serialized parts in the package.

Source code in src/docx/opc/pkgreader.py
def iter_sparts(self):
    """Generate a 4-tuple `(partname, content_type, reltype, blob)` for each of the
    serialized parts in the package."""
    for s in self._sparts:
        yield (s.partname, s.content_type, s.reltype, s.blob)

iter_srels

iter_srels()

Generate a 2-tuple (source_uri, srel) for each of the relationships in the package.

Source code in src/docx/opc/pkgreader.py
def iter_srels(self):
    """Generate a 2-tuple `(source_uri, srel)` for each of the relationships in the
    package."""
    for srel in self._pkg_srels:
        yield (PACKAGE_URI, srel)
    for spart in self._sparts:
        for srel in spart.srels:
            yield (spart.partname, srel)

_ContentTypeMap

_ContentTypeMap()

Value type providing dictionary semantics for looking up content type by part name, e.g. content_type = cti['/ppt/presentation.xml'].

Source code in src/docx/opc/pkgreader.py
def __init__(self):
    super(_ContentTypeMap, self).__init__()
    self._overrides = CaseInsensitiveDict()
    self._defaults = CaseInsensitiveDict()

from_xml staticmethod

from_xml(content_types_xml)

Return a new _ContentTypeMap instance populated with the contents of content_types_xml.

Source code in src/docx/opc/pkgreader.py
@staticmethod
def from_xml(content_types_xml):
    """Return a new |_ContentTypeMap| instance populated with the contents of
    `content_types_xml`."""
    types_elm = parse_xml(content_types_xml)
    ct_map = _ContentTypeMap()
    for o in types_elm.overrides:
        ct_map._add_override(o.partname, o.content_type)
    for d in types_elm.defaults:
        ct_map._add_default(d.extension, d.content_type)
    return ct_map

_SerializedPart

_SerializedPart(
    partname, content_type, reltype, blob, srels
)

Value object for an OPC package part.

Provides access to the partname, content type, blob, and serialized relationships for the part.

Source code in src/docx/opc/pkgreader.py
def __init__(self, partname, content_type, reltype, blob, srels):
    super(_SerializedPart, self).__init__()
    self._partname = partname
    self._content_type = content_type
    self._reltype = reltype
    self._blob = blob
    self._srels = srels

reltype property

reltype

The referring relationship type of this part.

_SerializedRelationship

_SerializedRelationship(baseURI, rel_elm)

Value object representing a serialized relationship in an OPC package.

Serialized, in this case, means any target part is referred to via its partname rather than a direct link to an in-memory Part object.

Source code in src/docx/opc/pkgreader.py
def __init__(self, baseURI, rel_elm):
    super(_SerializedRelationship, self).__init__()
    self._baseURI = baseURI
    self._rId = rel_elm.rId
    self._reltype = rel_elm.reltype
    self._target_mode = rel_elm.target_mode
    self._target_ref = rel_elm.target_ref

is_external property

is_external

True if target_mode is RTM.EXTERNAL

reltype property

reltype

Relationship type, like RT.OFFICE_DOCUMENT

rId property

rId

Relationship id, like 'rId9', corresponds to the Id attribute on the CT_Relationship element.

target_mode property

target_mode

String in TargetMode attribute of CT_Relationship element, one of RTM.INTERNAL or RTM.EXTERNAL.

target_ref property

target_ref

String in Target attribute of CT_Relationship element, a relative part reference for internal target mode or an arbitrary URI, e.g. an HTTP URL, for external target mode.

target_partname property

target_partname

PackURI instance containing partname targeted by this relationship.

Raises ValueError on reference if target_mode is 'External'. Use target_mode to check before referencing.

_SerializedRelationships

_SerializedRelationships()

Read-only sequence of _SerializedRelationship instances corresponding to the relationships item XML passed to constructor.

Source code in src/docx/opc/pkgreader.py
def __init__(self):
    super(_SerializedRelationships, self).__init__()
    self._srels = []

drop

drop(srel)

Remove srel from this collection.

Used to discard a relationship whose target part is missing from the package, so the collection never hands out an rId that cannot be resolved to a part.

Source code in src/docx/opc/pkgreader.py
def drop(self, srel):
    """Remove `srel` from this collection.

    Used to discard a relationship whose target part is missing from the package, so
    the collection never hands out an rId that cannot be resolved to a part.
    """
    self._srels.remove(srel)

load_from_xml staticmethod

load_from_xml(baseURI, rels_item_xml)

Return _SerializedRelationships instance loaded with the relationships contained in rels_item_xml.

Returns an empty collection if rels_item_xml is None.

Source code in src/docx/opc/pkgreader.py
@staticmethod
def load_from_xml(baseURI, rels_item_xml):
    """Return |_SerializedRelationships| instance loaded with the relationships
    contained in `rels_item_xml`.

    Returns an empty collection if `rels_item_xml` is |None|.
    """
    srels = _SerializedRelationships()
    if rels_item_xml is not None:
        rels_elm = parse_xml(rels_item_xml)
        for rel_elm in rels_elm.Relationship_lst:
            srels._srels.append(_SerializedRelationship(baseURI, rel_elm))
    return srels