ocpsvg
raw JSON → 0.6.0 verified Sat May 09 auth: no python
A Python library for parsing, manipulating, and rendering SVG (Scalable Vector Graphics) files with support for OCP (Open Canvas Protocol) extensions. Version 0.6.0, active development with no fixed release cadence.
pip install ocpsvg Common errors
error ImportError: cannot import name 'SVG' from 'ocpsvg' ↓
cause Wrong import path or older version without 'SVG' class.
fix
Ensure you have version 0.6.0 or later: pip install --upgrade ocpsvg. Then use: from ocpsvg import SVG
error TypeError: 'module' object is not callable ↓
cause Trying to use 'ocpsvg' as a class instead of importing the SVG class.
fix
Correct: from ocpsvg import SVG; svg = SVG.from_file(...). Wrong: import ocpsvg; svg = ocpsvg.SVG.from_file(...) - not wrong but less explicit.
error lxml.etree.XMLSyntaxError: Document is empty, line 1, column 1 ↓
cause Passing an empty string or non-XML file to SVG.from_file().
fix
Ensure the file path is correct and contains valid SVG XML.
Warnings
gotcha SVG.from_file() returns an SVG object, not a string. Use .tostring() to get the XML string, not str() or repr(). ↓
fix Use svg.tostring() instead of print(svg) to serialize XML.
gotcha CSS selectors via cssselect() return a list of lxml elements, not SVG-specific objects. Direct attribute manipulation on returned elements works fine. ↓
fix Remember that the returned elements are standard lxml elements; use .set() and .get() for attributes.
Imports
- SVG
from ocpsvg import SVG
Quickstart
from ocpsvg import SVG
# Load an SVG file
svg = SVG.from_file('example.svg')
# Access SVG elements (using CSS selectors)
rect = svg.cssselect('rect')[0]
print(rect.attrib)
# Modify attributes
rect.set('fill', 'blue')
# Get the XML string
print(svg.tostring())