def iter_block_items(self): """ Generate a reference to each of the block-level content elements in this cell, in the order they appear. """ block_item_tags = (qn('w:p'), qn('w:tbl'), qn('w:sdt')) for child in self: if child.tag in block_item_tags: yield child
def clear_content(self): """ Remove all child elements, except the ``<w:pPr>`` element if present. """ for child in self[:]: if child.tag == qn('w:pPr'): continue self.remove(child)
def get_child_element(obj): child = obj.find(qn(self._nsptagname)) if child is None: raise InvalidXmlError( "required ``<%s>`` child element not present" % self._nsptagname ) return child
def remove_all(self, *tagnames): """ Remove all child elements whose tagname (e.g. 'a:p') appears in *tagnames*. """ for tagname in tagnames: matching = self.findall(qn(tagname)) for child in matching: self.remove(child)
def _set_element_datetime(self, prop_name, value): """ Set date/time value of child element having *prop_name* to *value*. """ if not isinstance(value, datetime): tmpl = ( "property requires <type 'datetime.datetime'> object, got %s") raise ValueError(tmpl % type(value)) element = self._get_or_add(prop_name) dt_str = value.strftime('%Y-%m-%dT%H:%M:%SZ') element.text = dt_str if prop_name in ('created', 'modified'): # These two require an explicit 'xsi:type="dcterms:W3CDTF"' # attribute. The first and last line are a hack required to add # the xsi namespace to the root element rather than each child # element in which it is referenced self.set(qn('xsi:foo'), 'bar') element.set(qn('xsi:type'), 'dcterms:W3CDTF') del self.attrib[qn('xsi:foo')]
def test_x_copyelement(testtree): from docxx.oxml.ns import qn, nsmap root2 = etree.Element(qn("w:body")) copy_element(root2, testtree, query("w:p")) move_element(root2, testtree, query("w:section")) assert len(root2.find("w:p", nsmap)) == 3 assert find_element(root2.find("w:section", nsmap), query("w:grid", x="20")) is not None assert len(testtree.find("w:p", nsmap)) == 3 assert find_element(testtree, query("w:section")) is None
def first_child_found_in(self, *tagnames): """ Return the first child found with tag in *tagnames*, or None if not found. """ for tagname in tagnames: child = self.find(qn(tagname)) if child is not None: return child return None
def val(self): """ The underline type corresponding to the ``w:val`` attribute value. """ val = self.get(qn('w:val')) underline = WD_UNDERLINE.from_xml(val) if underline == WD_UNDERLINE.SINGLE: return True if underline == WD_UNDERLINE.NONE: return False return underline
def val(self, value): # works fine without these two mappings, but only because True == 1 # and False == 0, which happen to match the mapping for WD_UNDERLINE # .SINGLE and .NONE respectively. if value is True: value = WD_UNDERLINE.SINGLE elif value is False: value = WD_UNDERLINE.NONE val = WD_UNDERLINE.to_xml(value) self.set(qn('w:val'), val)
def _remove_trailing_empty_p(self): """ Remove the last content element from this cell if it is an empty ``<w:p>`` element. """ block_items = list(self.iter_block_items()) last_content_elm = block_items[-1] if last_content_elm.tag != qn('w:p'): return p = last_content_elm if len(p.r_lst) > 0: return self.remove(p)
def testtree(): from docxx.oxml.ns import qn root = etree.Element(qn("w:body")) # p1 = etree.SubElement(root, qn("w:p")) # # child1 = etree.SubElement(p1, qn("w:r")) child1.text = "Apple" child2 = etree.SubElement(p1, qn("w:r")) child2.text = "Banana" child3 = etree.SubElement(p1, qn("w:r")) child3.text = "Orange" # sec = etree.SubElement(root, qn("w:section")) # # dir = etree.SubElement(sec, qn("w:direction")) dir.set("orientation", "vertical") grid = etree.SubElement(sec, qn("w:grid")) grid.set("x", "20") grid.set("y", "50") return root
def get_child_element_list(obj): return obj.findall(qn(self._nsptagname))
def _clark_name(self): if ':' in self._attr_name: return qn(self._attr_name) return self._attr_name
def getter_fixture(self, request): choice_tag = request.param parent = self.parent_bldr(choice_tag).element tagname = 'w:%s' % choice_tag expected_choice = parent.find(qn(tagname)) # None if not found return parent, expected_choice
def getter_fixture(self, request): zooChild_is_present = request.param parent = self.parent_bldr(zooChild_is_present).element zooChild = parent.find(qn('w:zooChild')) # None if not found return parent, zooChild
def getter_fixture(self): parent = self.parent_bldr(True).element zomChild = parent.find(qn('w:zomChild')) return parent, zomChild
def getter_fixture(self): parent = a_parent().with_nsdecls().with_child(an_oooChild()).element oooChild = parent.find(qn('w:oooChild')) return parent, oooChild
def it_determines_class_used_for_elements_with_matching_tagname( self, xml_text): register_element_cls('a:foo', CustElmCls) foo = parse_xml(xml_text) assert type(foo) is CustElmCls assert type(foo.find(qn('a:bar'))) is etree._Element
def test_x_query(testtree): from docxx.oxml.ns import nsmap, qn assert query(testtree)(testtree) assert query("w:body")(testtree) assert query(qn("w:body"))(testtree) assert query("w:grid", x="20")(testtree.find("w:section/w:grid", nsmap))
def test_x_namespaces(): from docxx.oxml.ns import qn from docxx.element import docx_ns assert docx_ns.qname("w:p") == qn("w:p") assert docx_ns.short_name(qn("w:p")) == "w:p"
def get_contenttype(cls, child): for idx, tg in enumerate(cls.content_tags): if child.tag in qn(tg): return idx return -1
def first_fixture(self, request): present, matching, match = request.param element = self.rPr_bldr(present).element tagnames = self.nsptags(matching) matching_child = element.find(qn('w:%s' % match)) if match else None return element, tagnames, matching_child