SVGWrite

1.4.3 · maintenance · verified Sat Apr 11

SVGWrite is a Python library for programmatically creating SVG (Scalable Vector Graphics) drawings. It enables developers to generate vector graphic shapes, text, and images as XML files. The library is currently in maintenance mode (version 1.4.3, last updated July 14, 2022), meaning it receives bug fixes but no new feature development. It focuses solely on writing SVG files and does not support reading or modifying existing SVG documents, though it can embed other SVGs as images.

Warnings

Install

Imports

Quickstart

This example creates a simple SVG file named 'test.svg' with a line and some red text. It demonstrates the basic steps of initializing a `Drawing` object, adding elements like `line` and `text`, and saving the output.

import svgwrite

def create_simple_svg(filename='test.svg'):
    dwg = svgwrite.Drawing(filename, profile='tiny', size=('200px', '100px'))

    # Add a line
    dwg.add(dwg.line((0, 0), (100, 50), stroke=svgwrite.rgb(10, 10, 16, '%')))

    # Add text
    text_element = dwg.text('Hello svgwrite!', insert=(10, 70), fill='red')
    text_element.set_font_size('20px')
    dwg.add(text_element)

    dwg.save()
    print(f"SVG saved to {filename}")

if __name__ == '__main__':
    create_simple_svg()

view raw JSON →