Skip to content

phys_pkg

phys_pkg

Provides a general interface to a physical OPC package, such as a zip file.

PhysPkgReader

Factory for physical package reader objects.

PhysPkgWriter

Factory for physical package writer objects.

_DirPkgReader

_DirPkgReader(path)

Bases: PhysPkgReader

Implements PhysPkgReader interface for an OPC package extracted into a directory.

path is the path to a directory containing an expanded package.

Source code in src/docx/opc/phys_pkg.py
def __init__(self, path):
    """`path` is the path to a directory containing an expanded package."""
    super(_DirPkgReader, self).__init__()
    self._path = os.path.abspath(path)

content_types_xml property

content_types_xml

Return the [Content_Types].xml blob from the package.

blob_for

blob_for(pack_uri)

Return contents of file corresponding to pack_uri in package directory.

Source code in src/docx/opc/phys_pkg.py
def blob_for(self, pack_uri):
    """Return contents of file corresponding to `pack_uri` in package directory."""
    path = os.path.join(self._path, pack_uri.membername)
    with open(path, "rb") as f:
        blob = f.read()
    return blob

contains

contains(pack_uri)

True if a member corresponding to pack_uri is present in the package.

Source code in src/docx/opc/phys_pkg.py
def contains(self, pack_uri):
    """True if a member corresponding to `pack_uri` is present in the package."""
    return os.path.isfile(os.path.join(self._path, pack_uri.membername))

close

close()

Provides interface consistency with ZipFileSystem, but does nothing, a directory file system doesn't need closing.

Source code in src/docx/opc/phys_pkg.py
def close(self):
    """Provides interface consistency with |ZipFileSystem|, but does nothing, a
    directory file system doesn't need closing."""
    pass

rels_xml_for

rels_xml_for(source_uri)

Return rels item XML for source with source_uri, or None if the item has no rels item.

Source code in src/docx/opc/phys_pkg.py
def rels_xml_for(self, source_uri):
    """Return rels item XML for source with `source_uri`, or None if the item has no
    rels item."""
    try:
        rels_xml = self.blob_for(source_uri.rels_uri)
    except IOError:
        rels_xml = None
    return rels_xml

_ZipPkgReader

_ZipPkgReader(pkg_file)

Bases: PhysPkgReader

Implements PhysPkgReader interface for a zip file OPC package.

Source code in src/docx/opc/phys_pkg.py
def __init__(self, pkg_file):
    super(_ZipPkgReader, self).__init__()
    # -- ZipFile() reads from the stream before it decides the file is not a zip,
    # -- so note where the caller left it and put it back on the failure path --
    try:
        origin = pkg_file.tell() if hasattr(pkg_file, "tell") else None
    except (OSError, ValueError):
        origin = None
    try:
        self._zipf = ZipFile(pkg_file, "r")
    except BadZipFile as err:
        # -- a truncated, garbage or encrypted file lands here; BadZipFile leaks an
        # -- implementation detail of this layer, so translate it --
        error = _not_a_package_error(pkg_file)
        if origin is not None:
            pkg_file.seek(origin)
        raise error from err

content_types_xml property

content_types_xml

Return the [Content_Types].xml blob from the zip package.

blob_for

blob_for(pack_uri)

Return blob corresponding to pack_uri.

Raises ValueError if no matching member is present in zip archive.

Source code in src/docx/opc/phys_pkg.py
def blob_for(self, pack_uri):
    """Return blob corresponding to `pack_uri`.

    Raises |ValueError| if no matching member is present in zip archive.
    """
    return self._zipf.read(pack_uri.membername)

close

close()

Close the zip archive, releasing any resources it is using.

Source code in src/docx/opc/phys_pkg.py
def close(self):
    """Close the zip archive, releasing any resources it is using."""
    self._zipf.close()

contains

contains(pack_uri)

True if a member corresponding to pack_uri is present in the archive.

Source code in src/docx/opc/phys_pkg.py
def contains(self, pack_uri):
    """True if a member corresponding to `pack_uri` is present in the archive."""
    return pack_uri.membername in self._membernames

rels_xml_for

rels_xml_for(source_uri)

Return rels item XML for source with source_uri or None if no rels item is present.

Source code in src/docx/opc/phys_pkg.py
def rels_xml_for(self, source_uri):
    """Return rels item XML for source with `source_uri` or None if no rels item is
    present."""
    try:
        rels_xml = self.blob_for(source_uri.rels_uri)
    except KeyError:
        rels_xml = None
    return rels_xml

_ZipPkgWriter

_ZipPkgWriter(pkg_file)

Bases: PhysPkgWriter

Implements PhysPkgWriter interface for a zip file OPC package.

Source code in src/docx/opc/phys_pkg.py
def __init__(self, pkg_file):
    super(_ZipPkgWriter, self).__init__()
    self._zipf = ZipFile(pkg_file, "w", compression=ZIP_DEFLATED)

close

close()

Close the zip archive, flushing any pending physical writes and releasing any resources it's using.

Source code in src/docx/opc/phys_pkg.py
def close(self):
    """Close the zip archive, flushing any pending physical writes and releasing any
    resources it's using."""
    self._zipf.close()

write

write(pack_uri, blob)

Write blob to this zip package with the membername corresponding to pack_uri.

Source code in src/docx/opc/phys_pkg.py
def write(self, pack_uri, blob):
    """Write `blob` to this zip package with the membername corresponding to
    `pack_uri`."""
    # -- a plain writestr() stamps each member with the current time, so saving
    # -- the same document twice produces different bytes. Use the zip epoch
    # -- instead; Word does not read these timestamps. --
    zinfo = ZipInfo(filename=pack_uri.membername, date_time=_ZIP_EPOCH)
    zinfo.compress_type = ZIP_DEFLATED
    self._zipf.writestr(zinfo, blob)