def load(self, fs): """ Retrieve [Content_Types].xml from specified file system and load it. Returns a reference to this _ContentTypesItem instance to allow generative call, e.g. ``cti = _ContentTypesItem().load(fs)``. """ element = fs.getelement('/[Content_Types].xml') defaults = element.findall(qname('ct','Default')) overrides = element.findall(qname('ct','Override')) self.__defaults = {d.get('Extension'): d.get('ContentType') for d in defaults} self.__overrides = {o.get('PartName'): o.get('ContentType') for o in overrides} return self
def element(self): nsmap = {None: pptx.spec.nsmap['ct']} element = etree.Element(qname('ct', 'Types'), nsmap=nsmap) if self.__defaults: for ext in sorted(self.__defaults.keys()): subelm = etree.SubElement(element, qname('ct', 'Default')) subelm.set('Extension', ext) subelm.set('ContentType', self.__defaults[ext]) if self.__overrides: for partname in sorted(self.__overrides.keys()): subelm = etree.SubElement(element, qname('ct', 'Override')) subelm.set('PartName', partname) subelm.set('ContentType', self.__overrides[partname]) return element
def __get_rel_elms(self, fs): """ Helper method for _load(). Return list of this relationship elements for this part from *fs*. Returns empty list if there are no relationships for this part, either because parts of this type never have relationships or its relationships are optional and none exist in this filesystem (package). """ relsitemURI = self.__relsitemURI(self.typespec, self.__partname, fs) if relsitemURI is None: return [] if relsitemURI not in fs: tmpl = "required relationships item '%s' not found in package" raise CorruptedPackageError(tmpl % relsitemURI) baseURI = os.path.split(self.__partname)[0] root_elm = fs.getelement(relsitemURI) return root_elm.findall(qname('pr','Relationship'))
def open(self, path): """Load the on-disk package located at *path*.""" fs = FileSystem(path) cti = _ContentTypesItem().load(fs) self.__relationships = [] # discard any rels from prior load parts_dict = {} # track loaded parts, graph is cyclic pkg_rel_elms = fs.getelement(Package.PKG_RELSITEM_URI)\ .findall(qname('pr','Relationship')) for rel_elm in pkg_rel_elms: rId = rel_elm.get('Id') reltype = rel_elm.get('Type') partname = '/%s' % rel_elm.get('Target') part = Part() parts_dict[partname] = part part._load(fs, partname, cti, parts_dict) rel = Relationship(rId, self, reltype, part) self.__relationships.append(rel) return self
def _relsitem_element(self): nsmap = {None: pptx.spec.nsmap['pr']} element = etree.Element(qname('pr', 'Relationships'), nsmap=nsmap) for rel in self.__relationships: element.append(rel._element) return element