Mess around with SVG styling, style, replace=False)

Add to or replace the CSS style in an SVG file.

  • svgfile (str) – Path to an SVG file or an SVG string.
  • newstyle (str) – CSS string, or path to CSS file.
  • replace (bool) – If true, replace the existing CSS with newstyle (default: False), properties)

Build a class string for an element using the properties. Class names take the form myclass_value. If a given class isn’t found in properties, the class name is added (e.g. myclass).

  • classes (Sequence) – Column names to include in the class list
  • properties (dict) – A single feature’s properties.

(list) class names, properties)

Build a data- attribute string for an element using the properties. Attributes take the form data_FIELD=PROPERTY.

  • datas (Sequence) – Column names to include in the class list
  • properties (dict) – A single feature’s properties.

(dict) attribute dictionary

Inline the CSS rules in an SVG. This is a very rough operation, and full css precedence rules won’t be respected. May ignore sibling operators (~, +), pseudo-selectors (e.g. :first-child), and attribute selectors (e.g. .foo[name=bar]). Works best with rules like:

  • .class
  • tag
  • tag.class
  • #layer .class
  • #layer tag
  • svg (string) – An SVG document.
  • style (string) – CSS to use, instead of the CSS in the <defs> element of the SVG.

Fetch a CSS string.

Parameters:style (str) – Either a CSS string or the path of a css file.

Replace one-line non-standard comments with body-style comments:

// non-standard comment
/* body-style comment */, factor)

Add a scale() operation to an entire svg file.

Make input safe of use in an svg ID or class field. Replaces blocks of whitespace with an underscore (_), deletes periods, number signs and double-quotes (.#"). If the first character isn’t an ascii letter, dash (-) or underscore (_), an underscore is added to the beginning.

Parameters:string (mixed) – Input to sanitize

Utilities for manipulating the DOM and applying styles to same


Escape an ampersand that isn’t already url encoded

svgis.dom.apply_rule(doc, rule, nsmap)

Apply a tinycss2 rule to an etree.Element :param doc: The svg document to scan. :type doc: ElementTree.Element :param rule: tinycss2 rule :type rule: QualifiedRule :param nsmap: namespace map :type nsmap: dict

svgis.dom.apply_rules(doc, rules, nsmap=None)

Apply tinycss2 rules to an etree.Element (only tested on documents created by SVGIS).

  • doc (ElementTree.Element) – The svg document to scan.
  • rules (list) – List of tinycss2 Rules to apply.
  • nsmap (dict) – namespace map. Default: { "svg": "" }

Except the content from CSS rules.


Convert the selector section of a CSS rule to string.

svgis.dom.serialize_token(token, previous_token=None)

Convert a tinycss2 selector to string in preparation for use with cssselect. This involves prepending ‘svg|’ to element selectors.

Parameters:token (tinycss2.ast.Node) – The selector to serialize.