Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created October 23, 2025 11:50
Show Gist options
  • Select an option

  • Save aspose-com-gists/aeed4086d2287379bbcad7a7090e2b2f to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/aeed4086d2287379bbcad7a7090e2b2f to your computer and use it in GitHub Desktop.
Change SVG Fill Color in Python using Aspose.SVG
import aspose.svg as svg
# 1. Load the SVG document
document = svg.SVGDocument("sample.svg")
# 2. Get root SVG element of the document
svgElement = document.root_element
# 3. Find all circle elements in g element
circleNodes = svgElement.query_selector("circle")
# 4. Get the first circle element and set fill attributes
circleNodes.set_attribute("fill", "#0F0") # bright green
circleNodes.set_attribute("fill-opacity", "0.3") # 30% opacity
# 5. Save the updated SVG
document.save("sample_updated.svg")
import aspose.svg as svg
# 1. Load the SVG document
document = svg.SVGDocument("simple-path.svg")
# 2. Get root SVG element of the document
svgElement = document.root_element
# 3. Find all circle elements in g element
circle_element = svgElement.query_selector("path:nth-child(1)")
# 4. Get the first circle element and set fill attributes
circle_element.set_attribute("fill", "#0F0") # bright green
circle_element.set_attribute("fill-opacity", "0.3") # 30% opacity
# 5. Save the updated SVG
document.save("simple-path-updated.svg")
import aspose.svg as svg
# 1. Load an existing SVG
document = svg.SVGDocument("sample.svg")
# 2. Root element
root = document.root_element
# 3. Pick the element you want to color (choose one selector)
target = root.query_selector("circle") # or "path", ".some-class", "#myPath"
# 4. Apply fill color using style attribute
target.set_attribute("style", "fill:blue")
# 5. Save result
document.save("ApplyStyle.svg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment