def setUp(self): tr_xml = ( '<a:tr %s h="370840"><a:tc><a:txBody><a:p/></a:txBody></a:tc><a:t' 'c><a:txBody><a:p/></a:txBody></a:tc></a:tr>' % nsdecls('a') ) test_tr_elm = parse_xml_bytes(tr_xml) self.cells = _CellCollection(test_tr_elm, None)
def setUp(self): tbl_xml = ( '<a:tbl %s><a:tblGrid><a:gridCol w="3048000"/><a:gridCol w="30480' '00"/></a:tblGrid></a:tbl>' % nsdecls('a') ) test_tbl_elm = parse_xml_bytes(tbl_xml) self.columns = _ColumnCollection(test_tbl_elm, Mock(name='table'))
def new_tbl(rows, cols, width, height, tableStyleId=None): """Return a new ``<p:tbl>`` element tree""" # working hypothesis is this is the default table style GUID if tableStyleId is None: tableStyleId = '{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}' xml = CT_Table._tbl_tmpl % (tableStyleId) tbl = parse_xml_bytes(xml) # add specified number of rows and columns rowheight = height/rows colwidth = width/cols for col in range(cols): # adjust width of last col to absorb any div error if col == cols-1: colwidth = width - ((cols-1) * colwidth) SubElement(tbl.tblGrid, 'a:gridCol', w=str(colwidth)) for row in range(rows): # adjust height of last row to absorb any div error if row == rows-1: rowheight = height - ((rows-1) * rowheight) tr = SubElement(tbl, 'a:tr', h=str(rowheight)) for col in range(cols): tr.append(CT_TableCell.new_tc()) objectify.deannotate(tbl, cleanup_namespaces=True) return tbl
def setUp(self): tr_xml = ( '<a:tr %s h="370840"><a:tc><a:txBody><a:p/></a:txBody></a:tc><a:t' 'c><a:txBody><a:p/></a:txBody></a:tc></a:tr>' % nsdecls('a') ) test_tr_elm = parse_xml_bytes(tr_xml) self.row = _Row(test_tr_elm, Mock(name='table'))
def new_placeholder_sp(id_, name, ph_type, orient, sz, idx): """ Return a new ``<p:sp>`` element tree configured as a placeholder shape. """ xml = CT_Shape._ph_sp_tmpl % (id_, name) sp = parse_xml_bytes(xml) # placeholder shapes get a "no group" lock SubElement(sp.nvSpPr.cNvSpPr, 'a:spLocks') sp.nvSpPr.cNvSpPr[qn('a:spLocks')].set('noGrp', '1') # placeholder (ph) element attributes values vary by type ph = SubElement(sp.nvSpPr.nvPr, 'p:ph') if ph_type != PH_TYPE_OBJ: ph.set('type', ph_type) if orient != PH_ORIENT_HORZ: ph.set('orient', orient) if sz != PH_SZ_FULL: ph.set('sz', sz) if idx != 0: ph.set('idx', str(idx)) placeholder_types_that_have_a_text_frame = ( PH_TYPE_TITLE, PH_TYPE_CTRTITLE, PH_TYPE_SUBTITLE, PH_TYPE_BODY, PH_TYPE_OBJ) if ph_type in placeholder_types_that_have_a_text_frame: sp.append(CT_TextBody.new_txBody()) objectify.deannotate(sp, cleanup_namespaces=True) return sp
def setUp(self): tbl_xml = ( '<a:tbl %s><a:tr h="370840"><a:tc><a:txBody><a:p/></a:txBody></a:' 'tc><a:tc><a:txBody><a:p/></a:txBody></a:tc></a:tr><a:tr h="37084' '0"><a:tc><a:txBody><a:p/></a:txBody></a:tc><a:tc><a:txBody><a:p/' '></a:txBody></a:tc></a:tr></a:tbl>' % nsdecls('a') ) test_tbl_elm = parse_xml_bytes(tbl_xml) self.rows = _RowCollection(test_tbl_elm, Mock(name='table'))
def new_textbox_sp(id_, name, left, top, width, height): """ Return a new ``<p:sp>`` element tree configured as a base textbox shape. """ xml = CT_Shape._textbox_sp_tmpl % (id_, name, left, top, width, height) sp = parse_xml_bytes(xml) objectify.deannotate(sp, cleanup_namespaces=True) return sp
def it_raises_when_shape_type_called_on_unrecognized_shape(self): xml = ( '<p:sp xmlns:p="http://schemas.openxmlformats.org/presentationml/' '2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/' '2006/main"><p:nvSpPr><p:cNvPr id="9" name="Unknown Shape Type 8"' '/><p:cNvSpPr/><p:nvPr/></p:nvSpPr><p:spPr/></p:sp>') sp = parse_xml_bytes(xml) shape = Shape(sp, None) # verify ----------------------- with pytest.raises(NotImplementedError): shape.shape_type
def new_pic(id_, name, desc, rId, left, top, width, height): """ Return a new ``<p:pic>`` element tree configured with the supplied parameters. """ xml = CT_Picture._pic_tmpl % (id_, name, desc, rId, left, top, width, height) pic = parse_xml_bytes(xml) objectify.deannotate(pic, cleanup_namespaces=True) return pic
def new_graphicFrame(id_, name, left, top, width, height): """ Return a new ``<p:graphicFrame>`` element tree suitable for containing a table or chart. Note that a graphicFrame element is not a valid shape until it contains a graphical object such as a table. """ xml = CT_GraphicalObjectFrame._graphicFrame_tmpl % ( id_, name, left, top, width, height) graphicFrame = parse_xml_bytes(xml) objectify.deannotate(graphicFrame, cleanup_namespaces=True) return graphicFrame
def it_raises_when_shape_type_called_on_unrecognized_shape(self): xml = ( '<p:sp xmlns:p="http://schemas.openxmlformats.org/presentationml/' '2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/' '2006/main"><p:nvSpPr><p:cNvPr id="9" name="Unknown Shape Type 8"' '/><p:cNvSpPr/><p:nvPr/></p:nvSpPr><p:spPr/></p:sp>' ) sp = parse_xml_bytes(xml) shape = Shape(sp, None) # verify ----------------------- with pytest.raises(NotImplementedError): shape.shape_type
def but_accepts_unicode_providing_there_is_no_encoding_declaration(self): non_enc_decl = '<?xml version="1.0" standalone="yes"?>' enc_decl = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' xml_body = "<foo><bar>føøbår</bar></foo>" # unicode body by itself doesn't raise parse_xml_bytes(xml_body) # adding XML decl without encoding attr doesn't raise either xml_text = "%s\n%s" % (non_enc_decl, xml_body) parse_xml_bytes(xml_text) # but adding encoding in the declaration raises ValueError xml_text = "%s\n%s" % (enc_decl, xml_body) with pytest.raises(ValueError): parse_xml_bytes(xml_text)
def but_accepts_unicode_providing_there_is_no_encoding_declaration(self): non_enc_decl = '<?xml version="1.0" standalone="yes"?>' enc_decl = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' xml_body = '<foo><bar>føøbår</bar></foo>' # unicode body by itself doesn't raise parse_xml_bytes(xml_body) # adding XML decl without encoding attr doesn't raise either xml_text = '%s\n%s' % (non_enc_decl, xml_body) parse_xml_bytes(xml_text) # but adding encoding in the declaration raises ValueError xml_text = '%s\n%s' % (enc_decl, xml_body) with pytest.raises(ValueError): parse_xml_bytes(xml_text)
def p_with_text(self, p_with_text_xml): return parse_xml_bytes(p_with_text_xml)
def cell(self): return parse_xml_bytes(test_table_xml.cell)
def cell_with_margins(self): return parse_xml_bytes(test_table_xml.cell_with_margins)
def textbox(self): return parse_xml_bytes(test_shape_xml.textbox)
def placeholder(self): return parse_xml_bytes(test_shape_xml.placeholder)
def autoshape(self): return parse_xml_bytes(test_shape_xml.autoshape)
def element(self): """Return element based on XML generated by builder""" return parse_xml_bytes(self.xml)
def r(self, r_xml): return parse_xml_bytes(r_xml)
def it_prefers_to_parse_bytes(self, xml_bytes): parse_xml_bytes(xml_bytes)
def it_uses_oxml_configured_parser_to_parse_xml( self, mock_xml_bytes, fromstring, mock_oxml_parser): element = parse_xml_bytes(mock_xml_bytes) fromstring.assert_called_once_with(mock_xml_bytes, mock_oxml_parser) assert element is fromstring.return_value
def element(self): """ Element parsed from XML generated by builder in current state """ elm = parse_xml_bytes(self.xml()) return elm
def isolated_tbl_with_true_props(self): return parse_xml_bytes(test_table_xml.isolated_tbl_with_true_props)
def load(cls, partname, content_type, blob, package): slide_elm = parse_xml_bytes(blob) slide = cls(partname, content_type, slide_elm, package) return slide
def new_coreProperties(): """Return a new ``<cp:coreProperties>`` element""" xml = CT_CoreProperties._coreProperties_tmpl coreProperties = parse_xml_bytes(xml) return coreProperties
def top_aligned_cell(self): return parse_xml_bytes(test_table_xml.top_aligned_cell)
def empty_spTree(self): return parse_xml_bytes(test_shape_xml.empty_spTree)
def rounded_rectangle(self): return parse_xml_bytes(test_shape_xml.rounded_rectangle)
def isolated_tbl(self): return parse_xml_bytes(test_table_xml.isolated_tbl)
def new_txBody(): """Return a new ``<p:txBody>`` element tree""" xml = CT_TextBody._txBody_tmpl txBody = parse_xml_bytes(xml) objectify.deannotate(txBody, cleanup_namespaces=True) return txBody
def new_tc(): """Return a new ``<a:tc>`` element tree""" xml = CT_TableCell._tc_tmpl tc = parse_xml_bytes(xml) objectify.deannotate(tc, cleanup_namespaces=True) return tc
def setUp(self): gridCol_xml = '<a:gridCol %s w="3048000"/>' % nsdecls('a') test_gridCol_elm = parse_xml_bytes(gridCol_xml) self.column = _Column(test_gridCol_elm, Mock(name='table'))