opc.package module#
- class opc.package.Package(path, parent=None)[source]#
Class to handle the office open xml packages as per Open Package Convention
- Parameters:
path – path of the package file
parent – parent object. default is None
Example to show how to read and write an opc package:
from opc import Package # create an object of package with path of the file and read contents package = Package("/some/path/to/presentation.pptx").read() # do some changes to the content of the package # write the package to a file package.write("/some/other/path/to/saved.pptx")
- add_part(uri_str, type_)[source]#
Constructs a
PartorRelsPartobject and add to the parts collection of thePackage. part or relspart is decided based on the uri_str value, if uri_str ends with .rels relspart is constructed otherwise part is constructed.If there is hook available for the given type then that is called after the part is constructed. the return value of the callback hook is assigned to the _typeobj of the part. It is callback hook’s responsibility to keep the reference to the part object which is passed to the callback hook call.
- Parameters:
uri_str – string value of uri
type – type of the part object
- Returns:
Partobject- Raises:
ValueError – if part already exists with given uri_str
- exists_part(uri_str)[source]#
Checks if
Partexists in thePackage- Parameters:
uri_str – uri string
- Returns:
True | False
- get_part(uri_str)[source]#
Gets part having the given uri from the package
- Parameters:
uri_str – string value of the uri
- Returns:
Partobject with given uri
- get_parts(type_)[source]#
Gets list of parts of the given content type
- Parameters:
type – content type of the part
- Returns:
list of parts with the given type
- read()[source]#
Reads the package file and constructs
Typesobject andPartof the package and then read the contents of the parts
- register_part_hook(type_, callback)[source]#
Registers a callback hook to the content type. Hooks are called when part is created and before read method is called. Any existing hook on the given type will be over written
- Parameters:
type – content type of the part
callback – callable (class or method or function) accepts one arg
- remove_part(uri_str)[source]#
Removes a
Partfrom thePackage. Also removes entry from types if the type is not refering to any other part.- Parameters:
uri_str – string value of uri
- write(path)[source]#
Writes the package parts and content types to the given path
- Parameters:
path – path where package is to be written to.
- property core_properties#
Readonly property.
- Returns:
CorePropertiesobject of the package
- property path#
Readonly property.
- Returns:
Path of the package file