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(qtag("ct:Default")) overrides = element.findall(qtag("ct:Override")) self.__defaults = dict((d.get("Extension"), d.get("ContentType")) for d in defaults) self.__overrides = dict((o.get("PartName"), o.get("ContentType")) for o in overrides) return self
def element(self): nsmap = {None: pptx.spec.nsmap['ct']} element = etree.Element(qtag('ct:Types'), nsmap=nsmap) if self.__defaults: for ext in sorted(self.__defaults.keys()): subelm = etree.SubElement(element, qtag('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, qtag('ct:Override')) subelm.set('PartName', partname) subelm.set('ContentType', self.__overrides[partname]) return element
def element(self): nsmap = {None: pptx.spec.nsmap["ct"]} element = etree.Element(qtag("ct:Types"), nsmap=nsmap) if self.__defaults: for ext in sorted(self.__defaults.keys()): subelm = etree.SubElement(element, qtag("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, qtag("ct:Override")) subelm.set("PartName", partname) subelm.set("ContentType", self.__overrides[partname]) return element
def element(self): nsmap = {None: pptx.spec.nsmap['ct']} element = etree.Element(qtag('ct:Types'), nsmap=nsmap) if self._defaults: for ext in sorted(self._defaults.keys()): subelm = etree.SubElement(element, qtag('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, qtag('ct:Override')) subelm.set('PartName', partname) subelm.set('ContentType', self._overrides[partname]) return element
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(qtag('ct:Default')) overrides = element.findall(qtag('ct:Override')) self.__defaults = dict( (d.get('Extension'), d.get('ContentType')) for d in defaults) self.__overrides = dict( (o.get('PartName'), o.get('ContentType')) for o in overrides) return self
def __init__(self): super(CT_Transform2D, self).__init__() self._element = Element(qtag('a:xfrm'), nsmap=nsmap) self.off = CT_Point2D() self._element.append(self.off.element) self.ext = CT_PositiveSize2D() self._element.append(self.ext.element)
def __init__(self): super(CT_TextBody, self).__init__() self._element = Element(qtag('p:txBody'), nsmap=nsmap) self.bodyPr = CT_TextBodyProperties() self._element.append(self.bodyPr.element) self.lstStyle = CT_TextListStyle() self._element.append(self.lstStyle.element) self.p = CT_TextParagraph() self._element.append(self.p.element)
def __init__(self): super(CT_ShapeProperties, self).__init__() self._element = Element(qtag('p:spPr'), nsmap=nsmap) self.xfrm = CT_Transform2D() self._element.append(self.xfrm.element) self.prstGeom = CT_PresetGeometry2D() self._element.append(self.prstGeom.element) self.noFill = CT_NoFillProperties() self._element.append(self.noFill.element)
def __init__(self): super(CT_ShapeNonVisual, self).__init__() self._element = Element(qtag('p:nvSpPr'), nsmap=nsmap) self.cNvPr = CT_NonVisualDrawingProps() self._element.append(self.cNvPr.element) self.cNvSpPr = CT_NonVisualDrawingShapeProps() self._element.append(self.cNvSpPr.element) self.nvPr = CT_ApplicationNonVisualDrawingProps() self._element.append(self.nvPr.element)
def __init__(self): super(CT_Shape, self).__init__() self._element = Element(qtag('p:sp'), nsmap=nsmap) self.nvSpPr = CT_ShapeNonVisual() self._element.append(self.nvSpPr.element) self.spPr = CT_ShapeProperties() self._element.append(self.spPr.element) self.txBody = CT_TextBody() self._element.append(self.txBody.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) root_elm = fs.getelement(relsitemURI) return root_elm.findall(qtag('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(qtag("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) fs.close() return self
def test__blob_rewrites_sldIdLst(self): """Presentation._blob rewrites sldIdLst""" # setup ------------------------ rels = RelationshipCollectionBuilder() rels = rels.with_tuple_targets(2, RT_SLIDE_MASTER) rels = rels.with_tuple_targets(3, RT_SLIDE) rels = rels.with_ordering(RT_SLIDE_MASTER, RT_SLIDE) rels = rels.build() prs = Presentation() prs._relationships = rels prs.partname = '/ppt/presentation.xml' path = os.path.join(thisdir, 'test_files/presentation.xml') prs._element = oxml_parse(path).getroot() # exercise --------------------- blob = prs._blob # verify ----------------------- presentation = oxml_fromstring(blob) sldIds = presentation.xpath('./p:sldIdLst/p:sldId', namespaces=nsmap) expected = ['rId3', 'rId4', 'rId5'] actual = [sldId.get(qtag('r:id')) for sldId in sldIds] msg = "expected ordering %s, got %s" % (expected, actual) self.assertEqual(expected, actual, msg)
def open(self, file): """ Load the package contained in *file*, where *file* can be a path to a file or directory (a string), or a file-like object. If *file* is a path to a directory, the directory must contain an expanded package such as is produced by unzipping an OPC package file. """ fs = FileSystem(file) 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(qtag('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) fs.close() return self
def __init__(self): super(CT_GeomGuideList, self).__init__() self._element = Element(qtag('a:avLst'), nsmap=nsmap)
def __init__(self): super(CT_PresetGeometry2D, self).__init__() self._element = Element(qtag('a:prstGeom'), nsmap=nsmap) self.avLst = CT_GeomGuideList() self._element.append(self.avLst.element)
def _relsitem_element(self): nsmap = {None: pptx.spec.nsmap['pr']} element = etree.Element(qtag('pr:Relationships'), nsmap=nsmap) for rel in self.__relationships: element.append(rel._element) return element
def __init__(self): super(CT_TextBodyProperties, self).__init__() self._element = Element(qtag('a:bodyPr'), nsmap=nsmap) self.spAutoFit = CT_TextShapeAutofit() self._element.append(self.spAutoFit.element)
def __init__(self): super(CT_TextListStyle, self).__init__() self._element = Element(qtag('a:lstStyle'), nsmap=nsmap)
def __init__(self): super(CT_TextParagraph, self).__init__() self._element = Element(qtag('a:p'), nsmap=nsmap)
def __init__(self): super(CT_TextShapeAutofit, self).__init__() self._element = Element(qtag('a:spAutoFit'), nsmap=nsmap)
def __init__(self): super(CT_ApplicationNonVisualDrawingProps, self).__init__() self._element = Element(qtag('p:nvPr'), nsmap=nsmap)
def __init__(self): super(CT_NonVisualDrawingShapeProps, self).__init__() self._element = Element(qtag('p:cNvSpPr'), nsmap=nsmap)
def __init__(self): super(CT_PositiveSize2D, self).__init__() self._element = Element(qtag('a:ext'), nsmap=nsmap)
def __init__(self): super(CT_NoFillProperties, self).__init__() self._element = Element(qtag('a:noFill'), nsmap=nsmap)
def __init__(self): super(CT_Point2D, self).__init__() self._element = Element(qtag('a:off'), nsmap=nsmap)