opc.part module#

class opc.part.Part(parent, uri_str, type_)[source]#

Part class is not to be instantiated directly. Instead use add_part

Parameters:
  • parent – package object

  • uri_str – uri string

  • type – type of content of part

add_relspart()[source]#

adds the relspart to the package for this part. It also initializes the xml for the relspart. Do not call this method if relspart already exists for the current part.

Returns:

relspart object

get_abs_uri_str(target_rel_uri_str)[source]#

Method to get the absolute uri string value from the given relative uri string value of target part

Parameters:

target_rel_uri_str – relative uri string value of a target part

Returns:

absolute uri string value of a target part

Example:

abs_uri_str = presentation_part.get_abs_uri_str('slides/slide1.xml)
print(abs_uri_str)      # /ppt/slides/slide1.xml

Method to get the part object from the relationship id with respect to current part.

Parameters:

rid – relationship id

Returns:

the related Part of the current part from the relationship

Example:

related_part = presentation_part.get_related_part('rId2')
print(related_part.uri.str)     # /ppt/slides/slide1.xml

Gets list of parts that are related by given reltype wrt to current part

Parameters:

reltype – str value

Returns:

list of related parts

get_rels_part()[source]#

Method that gets the RelsPart of the current part

Returns:

RelsPart object or None

Example:

from opc import Package
package = Package("/some/path/to/presentation.pptx").read()
presentation_part = package.get_part('/ppt/presentation.xml')
print(presentation_part.uri.str)    # '/ppt/presentation.xml'

presentation_relspart = presentation_part.get_rels_part()
print(presentation_relspart.uri.str) 
    # /ppt/_rels/presentation.xml.rels
read(f)[source]#

reads the content from the file object. if typeobj is present then responsibility of reading the content is passed on to typeobj

Parameters:

f – file object

write(f)[source]#

writes the part content to the file object. if typeobj is present then responsibility of writing the content is passed on to typeobj

Parameters:

f – file object

property type#

Readonly property

Returns:

content type of the part

property typeobj#

Readonly property

Getter:

object created as per the content of the part using hook

Setter:

sets the typeobj property of part

property uri#

Readonly property

Returns:

Uri object of the part