OpcPackage
paper-pptx 0.2.0 API reference
Main API class for |python-opc|.
A new instance is constructed by calling the :meth:open classmethod with a path to a package
file or file-like object containing a package (.pptx file).
OpcPackage(pkg_file: str | IO[bytes])Attributes
main_document_part
attributemain_document_partPresentationPartReturn Part subtype serving as the main document part for this package.
In this case it will be a Presentation part.
Functions
drop_rel
funcdrop_rel(rId: str) -> NoneRemove relationship identified by rId.
paramrIdstrReturns
Noneiter_parts
funciter_parts() -> Iterator[Part]Generate exactly one reference to each part in the package.
Returns
typing.Iterator[pptx.opc.package.Part]iter_rels
funciter_rels() -> Iterator[_Relationship]Generate exactly one reference to each relationship in package.
Performs a depth-first traversal of the rels graph.
Returns
typing.Iterator[pptx.opc.package._Relationship]next_partname
funcnext_partname(tmpl: str) -> PackURIReturn PackURI next available partname matching tmpl.
tmpl is a printf (%)-style template string containing a single replacement item, a '%d'
to be used to insert the integer portion of the partname. Example:
'/ppt/slides/slide%d.xml'
paramtmplstrReturns
pptx.opc.packuri.PackURIopen
funcopen(pkg_file: str | IO[bytes]) -> SelfReturn an OpcPackage instance loaded with the contents of pkg_file.
parampkg_filestr | IO[bytes]Returns
typing_extensions.Selfpart_related_by
funcpart_related_by(reltype: str) -> PartReturn (single) part having relationship to this package of reltype.
Raises KeyError if no such relationship is found and ValueError if more than one such
relationship is found.
paramreltypestrReturns
pptx.opc.package.Partrelate_to
funcrelate_to(target: Part | str, reltype: str, is_external: bool = False) -> strReturn rId key of relationship of reltype to target.
If such a relationship already exists, its rId is returned. Otherwise the relationship is added and its new rId returned.
paramtargetPart | strparamreltypestrparamis_externalbool= FalseReturns
strrelated_part
funcrelated_part(rId: str) -> PartReturn related Part subtype identified by rId.
paramrIdstrReturns
pptx.opc.package.Partsave
funcsave(pkg_file: str | IO[bytes]) -> NoneSave this package to pkg_file.
pkg_file can be a filesystem path (str or os.PathLike) or a file-like object open
for writing bytes.
A path destination is written atomically: the package is serialized into a temporary
file in the destination's directory and moved into place with os.replace(), so a
failure part-way through leaves any existing file untouched. Symlinks are resolved, so
the file a link names is the file that is written.
A stream destination is serialized into a private staging buffer and then written
straight through, so no bytes reach the stream unless the whole package serialized. Only
write is required of the stream. The package is written at the stream's current
position; past that, bytes beyond the package are the caller's and are left alone.
What a failure during that copy costs depends on what the destination supports. One
that can be read, rewound and truncated has its prior contents and cursor restored, and
if that restore itself fails the original error is replaced by a RuntimeError carrying
it as __cause__. A write-only or unseekable sink keeps whatever partial package landed;
a caller writing to a pipe cannot expect otherwise. The same capability decides
truncation: a readable stream positioned at the start is truncated to the package, so no
tail of a previous document survives, while a write-only stream keeps that tail.
Atomic path writes are a deliberate departure from the v0 rule that save() behavior is
unchanged from upstream, accepted because upstream serialized directly into the
destination, so a mid-write failure on the ordinary prs.save(same_path) pattern
destroyed the deck being edited, irrecoverably.
The costs of replacing rather than overwriting: owner, group, ACLs, extended attributes and hard links do not survive (mode bits are carried over), and the destination's directory must be writable, not just the file.
parampkg_filestr | IO[bytes]Returns
Nonetarget_ref
functarget_ref(rId: str) -> strReturn URL contained in target ref of relationship identified by rId.
paramrIdstrReturns
str