Learn how to Change SVG Fill Color in Python using Aspose.SVG
Created
October 23, 2025 11:50
-
-
Save aspose-com-gists/aeed4086d2287379bbcad7a7090e2b2f to your computer and use it in GitHub Desktop.
Change SVG Fill Color in Python using Aspose.SVG
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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