Export and import¶
The format registry and its functions.
File formats, found by name or file extension, and the functions that export, save and load through them.
Built in, out: SVG, SVGZ, PNG, JPEG, WebP, BMP, GIF, PPM, TIFF, PDF (vector, via skia), TikZ,
EPS, DXF, HPGL, G-code, Excalidraw, HTML, ICO and ICNS. In: SVG, SVGZ, raster images (PNG,
JPEG, WebP, GIF, BMP, PNM), PDF, TikZ and EPS (their vector paths), DXF, HPGL, G-code,
Excalidraw, HTML, ICO and ICNS, text and Markdown, plus Graphviz DOT, Mermaid, PlantUML and D2
when their command-line tools are installed; with the office extra, PPTX (editable shapes,
in and out), DOCX (in and out) and XLSX (picture, out). Add a format by subclassing
Format; svg.formats lists them.
Example
>>> import svgwriter as svg
>>> doc = svg.Document(20, 10)
>>> _ = doc.rect(0, 0, 20, 10, fill="teal")
>>> {"svg", "png", "pdf", "tikz", "dxf"} <= set(svg.formats.list())
True
>>> svg.export.export(doc, ".pdf")[:5]
b'%PDF-'
- export(document: Document | Sequence[Document], format: str, *, seconds: float | None = None, **options: Any) bytes | str[source]¶
Convert document to format (a name or extension); returns
bytesorstr.- Parameters:
document (Document or sequence of Document) – The document to convert, or several documents for a multi-page format (
"pdf": one page each;"pptx": one slide each).format (str) – A registered format name or an extension such as
".pdf".seconds (float, optional) – Export the SMIL animation frame at this many seconds (see
Document.at()) instead of the document as written. Defaults to None, which keeps animations (formats without animation show t = 0).**options – Passed to the exporter function (see each format in
svg.formats).
- Returns:
bytes or str – The file contents; text formats return
str.- Raises:
ValueError – If no format writes format, the sequence is empty, or several documents are given to a single-page format.
See also
saveWrite the result to a file.
Examples
>>> import svgwriter as svg >>> doc = svg.Document(4, 4) >>> svg.export.export(doc, "png")[:4] == bytes.fromhex("89504e47") True >>> deck = svg.export.export([doc, svg.Document(8, 2)], "pdf") >>> len(svg.formats.reader("pdf").read(deck, page=None)) 2
- load(path: str | os.PathLike[str], format: str | None = None, **options: Any) Document | list[Document][source]¶
Read a document from path with the format chosen by format or the extension.
- Parameters:
- Returns:
Document or list of Document – The parsed document; relative image links inside it resolve against path. Multi-page formats return a list, one document per page, when their page option is None (
page=Nonefor PDF,slide=Nonefor PowerPoint).- Raises:
ValueError – If no format reads it (an
UnknownNameError).
See also
saveWrite a document.
Document.open_allEvery page of a file as a list.
Examples
>>> import tempfile >>> from pathlib import Path >>> import svgwriter as svg >>> with tempfile.TemporaryDirectory() as folder: ... target = svg.export.save(svg.Document(4, 4), Path(folder) / "a.svg") ... svg.export.load(target).root.tag 'svg'
- load_formats() None[source]¶
Import the built-in formats, so they register; the format registry calls it before its first lookup.
Examples
>>> from svgwriter.formats.export import load_formats >>> load_formats()
- save(document: Document | Sequence[Document], path: str | os.PathLike[str], format: str | None = None, *, seconds: float | None = None, **options: Any) Path[source]¶
Write document to path, choosing the format from format or the file extension.
- Parameters:
document (Document or sequence of Document) – The document to write, or several for a multi-page format (one PDF page or PowerPoint slide each).
path (str or path-like) – Destination file.
format (str, optional) – Format name overriding the extension. Defaults to None.
seconds (float, optional) – Save the SMIL animation frame at this many seconds instead of the animated document. Defaults to None.
**options – Passed to the format’s
write.
- Returns:
pathlib.Path – The file that was written.
- Raises:
ValueError – If no format writes it, or several documents are given to a single-page format.
Examples
>>> import tempfile >>> from pathlib import Path >>> import svgwriter as svg >>> with tempfile.TemporaryDirectory() as folder: ... target = svg.export.save(svg.Document(4, 4), Path(folder) / "a.svg") ... target.read_text(encoding="utf-8").startswith("<?xml") True