本文整理汇总了Python中openpyxl.xml.functions.Element类的典型用法代码示例。如果您正苦于以下问题:Python Element类的具体用法?Python Element怎么用?Python Element使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Element类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_cols
def write_cols(worksheet):
"""Write worksheet columns to xml.
<cols> may never be empty -
spec says must contain at least one child
"""
cols = []
for label, dimension in iteritems(worksheet.column_dimensions):
col_def = dict(dimension)
if col_def == {}:
continue
idx = column_index_from_string(label)
cols.append((idx, col_def))
if not cols:
return
el = Element('cols')
for idx, col_def in sorted(cols):
v = "%d" % idx
cmin = col_def.get('min') or v
cmax = col_def.get('max') or v
col_def.update({'min': cmin, 'max': cmax})
el.append(Element('col', col_def))
return el
示例2: to_tree
def to_tree(self, tagname):
"""
Return as XML node
"""
el = Element(tagname)
el.text = str(self)
return el
示例3: write_external_book_rel
def write_external_book_rel(book):
"""Serialise link to external file"""
root = Element("{%s}Relationships" % PKG_REL_NS)
attrs = {"Id":"rId1", "Target":book.Target, "TargetMode":book.TargetMode,
"Type":book.Type}
root.append(RelationElement(attrs))
return root
示例4: write_external_book_rel
def write_external_book_rel(book):
"""Serialise link to external file"""
root = Element("Relationships", xmlns=PKG_REL_NS)
rel = Relationship("", target=book.Target, targetMode=book.TargetMode, id="rId1")
rel.type = book.Type
root.append(rel.to_tree())
return root
示例5: write_comments_vml
def write_comments_vml(self):
root = Element("xml")
shape_layout = SubElement(root, "{%s}shapelayout" % officens,
{"{%s}ext" % vmlns: "edit"})
SubElement(shape_layout,
"{%s}idmap" % officens,
{"{%s}ext" % vmlns: "edit", "data": "1"})
shape_type = SubElement(root,
"{%s}shapetype" % vmlns,
{"id": "_x0000_t202",
"coordsize": "21600,21600",
"{%s}spt" % officens: "202",
"path": "m,l,21600r21600,l21600,xe"})
SubElement(shape_type, "{%s}stroke" % vmlns, {"joinstyle": "miter"})
SubElement(shape_type,
"{%s}path" % vmlns,
{"gradientshapeok": "t",
"{%s}connecttype" % officens: "rect"})
for idx, comment in enumerate(self.comments, 1026):
shape = _shape_factory()
col, row = coordinate_from_string(comment.ref)
row -= 1
column = column_index_from_string(col) - 1
shape.set('id', "_x0000_s%04d" % idx)
client_data = shape.find("{%s}ClientData" % excelns)
client_data.find("{%s}Row" % excelns).text = str(row)
client_data.find("{%s}Column" % excelns).text = str(column)
root.append(shape)
return tostring(root)
示例6: write
def write(self, root):
# Remove any existing comment shapes
if not hasattr(root, "findall"):
root = Element("xml")
comments = root.findall("{%s}shape" % vmlns)
for c in comments:
if c.get("type") == '#_x0000_t202':
root.remove(c)
# check whether comments shape type already exists
shape_types = root.findall("{%s}shapetype" % vmlns)
comments_type = False
for s in shape_types:
if s.get("id") == '_x0000_t202':
comments_type = True
break
if not comments_type:
self.add_comment_shapetype(root)
for idx, (coord, comment) in enumerate(self.comments, 1026):
self.add_comment_shape(root, idx, coord)
return tostring(root)
示例7: write_rels
def write_rels(worksheet, comments_id=None, vba_controls_id=None):
"""Write relationships for the worksheet to xml."""
root = Element('Relationships', xmlns=PKG_REL_NS)
rels = worksheet._rels
# VBA
if worksheet.vba_controls is not None:
rel = Relationship("vmlDrawing", id=worksheet.vba_controls,
target='/xl/drawings/vmlDrawing%s.vml' % vba_controls_id)
rels.append(rel)
# Comments
if worksheet._comment_count > 0:
rel = Relationship(type="comments", id="comments",
target='/xl/comments%s.xml' % comments_id)
rels.append(rel)
if worksheet.vba_controls is None:
rel = Relationship(type="vmlDrawing", id="commentsvml",
target='/xl/drawings/commentsDrawing%s.vml' % comments_id)
rels.append(rel)
for idx, rel in enumerate(rels, 1):
if rel.id is None:
rel.id = "rId{0}".format(idx)
root.append(rel.to_tree())
return root
示例8: _write_header
def _write_header(self):
"""
Generator that creates the XML file and the sheet header
"""
with xmlfile(self.filename) as xf:
with xf.element("worksheet", xmlns=SHEET_MAIN_NS):
if self.sheet_properties:
pr = write_sheetPr(self.sheet_properties)
xf.write(pr)
views = Element("sheetViews")
views.append(self.sheet_view.to_tree())
xf.write(views)
xf.write(write_format(self))
cols = write_cols(self)
if cols is not None:
xf.write(cols)
with xf.element("sheetData"):
try:
while True:
r = (yield)
xf.write(r)
except GeneratorExit:
pass
af = write_autofilter(self)
if af is not None:
xf.write(af)
if self._comments:
comments = Element("legacyDrawing", {"{%s}id" % REL_NS: "commentsvml"})
xf.write(comments)
示例9: to_tree
def to_tree(self, tagname=None, value=None, namespace=None):
namespace = getattr(self, "namespace", namespace)
if value is not None:
if namespace is not None:
tagname = "{%s}%s" % (namespace, tagname)
el = Element(tagname)
el.text = safe_string(value)
return el
示例10: to_tree
def to_tree(self, tagname=None, value=None, namespace=None):
namespace = getattr(self, "namespace", namespace)
if namespace is not None:
tagname = "{%s}%s" % (namespace, tagname)
el = Element(tagname)
if value is not None:
el.text = datetime_to_W3CDTF(value)
return el
示例11: _serialise_nested
def _serialise_nested(self, sequence):
"""
Colors need special handling
"""
for idx, color in enumerate(sequence):
stop = Element("stop", position=str(idx))
stop.append(color.to_tree())
yield stop
示例12: to_tree
def to_tree(self):
tree = Element("Relationships", xmlns=PKG_REL_NS)
for idx, rel in enumerate(self.Relationship, 1):
if not rel.Id:
rel.Id = "rId{0}".format(idx)
tree.append(rel.to_tree())
return tree
示例13: to_tree
def to_tree(self, tagname, obj, namespace=None):
tagname = namespaced(self, tagname, namespace)
container = Element(tagname)
if self.count:
container.set('count', str(len(obj)))
for v in obj:
container.append(v.to_tree())
return container
示例14: write_properties
def write_properties(worksheet, vba_attrs):
pr = Element('sheetPr', vba_attrs)
summary = Element('outlinePr',
summaryBelow='%d' % worksheet.show_summary_below,
summaryRight= '%d' % worksheet.show_summary_right)
pr.append(summary)
if worksheet.page_setup.fitToPage:
pr.append(Element('pageSetUpPr', fitToPage='1'))
return pr
示例15: write_pagebreaks
def write_pagebreaks(worksheet):
breaks = worksheet.page_breaks
if breaks:
tag = Element('rowBreaks', {'count': str(len(breaks)),
'manualBreakCount': str(len(breaks))})
for b in breaks:
tag.append(Element('brk', id=str(b), man="true", max='16383',
min='0'))
return tag