Skip to content

altchunk

altchunk

AltChunkPart and closely related objects.

AltChunkPart

AltChunkPart(
    partname: PackURI,
    content_type: str,
    blob: bytes | None = None,
    package: Package | None = None,
)

Bases: Part

An "alternative format import" part, the target of a w:altChunk reference.

Holds an embedded document in some format other than WordprocessingML — HTML, RTF, plain text, MHTML, or another .docx — for Word to convert and splice in when it opens the file. The bytes are stored and written back unchanged; this library has no knowledge of the embedded format.

Source code in src/docx/opc/part.py
def __init__(
    self,
    partname: PackURI,
    content_type: str,
    blob: bytes | None = None,
    package: Package | None = None,
):
    super(Part, self).__init__()
    self._partname = partname
    self._content_type = content_type
    self._blob = blob
    self._package = package

new classmethod

new(
    package: OpcPackage, blob: bytes, content_type: str
) -> AltChunkPart

An AltChunkPart newly created from blob and added to package.

Source code in src/docx/parts/altchunk.py
@classmethod
def new(cls, package: OpcPackage, blob: bytes, content_type: str) -> AltChunkPart:
    """An |AltChunkPart| newly created from `blob` and added to `package`."""
    ext = _EXT_FOR_CONTENT_TYPE.get(content_type, "bin")
    return cls(cls._next_partname(package, ext), content_type, blob, package)

new_from_stream classmethod

new_from_stream(
    package: OpcPackage,
    chunk: str | PathLike[str] | IO[bytes],
    content_type: str,
) -> AltChunkPart

An AltChunkPart newly created from chunk and added to package.

chunk is either a path to a file (a string or os.PathLike) or a file-like object open for binary read.

Source code in src/docx/parts/altchunk.py
@classmethod
def new_from_stream(
    cls,
    package: OpcPackage,
    chunk: str | os.PathLike[str] | IO[bytes],
    content_type: str,
) -> AltChunkPart:
    """An |AltChunkPart| newly created from `chunk` and added to `package`.

    `chunk` is either a path to a file (a string or ``os.PathLike``) or a file-like
    object open for binary read.
    """
    if isinstance(chunk, (str, os.PathLike)):
        with open(os.fspath(chunk), "rb") as f:
            blob = f.read()
    else:
        blob = chunk.read()
    return cls.new(package, blob, content_type)