BaseElement#

class svgwrite.base.BaseElement(**extra)#

The BaseElement is the root for all SVG elements. The SVG attributes are stored in attribs, and the SVG subelements are stored in elements.

BaseElement.__init__(**extra)#
参数:

extra

extra SVG attributes (keyword arguments)

  • add trailing ‘_’ to reserved keywords: 'class_', 'from_'

  • replace inner ‘-’ by ‘_’: 'stroke_width'

SVG attribute names will be checked, if debug is True.

workaround for removed attribs parameter in Version 0.2.2:

# replace
element = BaseElement(attribs=adict)

#by
element = BaseElement()
element.update(adict)

属性#

Attributes

BaseElement.attribs#

dict of SVG attributes

BaseElement.elements#

list of SVG subelements

方法#

Methods

BaseElement.add(element)#

Add an SVG element as subelement.

参数:

element – append this SVG element

返回:

the added element

BaseElement.tostring()#

Get the XML representation as unicode string.

返回:

unicode XML string of this object and all its subelements

BaseElement.get_xml()#

Get the XML representation as ElementTree object.

返回:

XML ElementTree of this object and all its subelements

BaseElement.get_id()#

Get the object id string, if the object does not have an id, a new id will be created.

返回:

string

BaseElement.get_iri()#

Get the IRI reference string of the object. (i.e., '#id').

返回:

string

BaseElement.get_funciri()#

Get the FuncIRI reference string of the object. (i.e. 'url(#id)').

返回:

string

BaseElement.update(attribs)#

Update SVG Attributes from dict attribs.

Rules for keys:

  1. trailing ‘_’ will be removed ('class_' -> 'class')

  2. inner ‘_’ will be replaced by ‘-’ ('stroke_width' -> 'stroke-width')

BaseElement.__getitem__(key)#

Get SVG attribute by key.

参数:

key (string) – SVG attribute name

返回:

SVG attribute value

BaseElement.__setitem__(key, value)#

Set SVG attribute by key to value.

参数:
  • key (string) – SVG attribute name

  • value (object) – SVG attribute value

BaseElement.set_desc(title=None, desc=None)#

Insert a title and/or a desc element as first subelement.

BaseElement.set_metadata(xmldata)#
参数:

xmldata – an xml.etree.ElementTree - Element() object.

set/get SVG attributes:

element['attribute'] = value
value = element['attribute']

attribs = {
  'class' = 'css-class',
  'stroke' = 'black',
}
element.update(attribs)

常见 SVG 属性#

Common SVG Attributes