Python binding to Typst, a new markup-based typesetting system that is powerful and easy to learn.
Actively maintained. Last release 4 weeks ago, commits in 7 of the last 26 weeks. A safe choice.
pip install typsttypst-py
Python binding to typst, a new markup-based typesetting system that is powerful and easy to learn.
Installation
Requires Python 3.9 or newer.
pip install typst
Usage
import datetime
import typst
# Compile `hello.typ` to PDF and save as `hello.pdf`
typst.compile("hello.typ", output="hello.pdf")
# Compile `hello.typ` to PNG and save as `hello.png`
typst.compile("hello.typ", output="hello.png", format="png", ppi=144.0)
# Or pass `hello.typ` content as bytes
with open("hello.typ", "rb") as f:
typst.compile(f.read(), output="hello.pdf")
# Or return PDF content as bytes
pdf_bytes = typst.compile("hello.typ")
# Use a fixed creation timestamp for reproducible PDFs
timestamp = datetime.datetime.fromtimestamp(0, datetime.timezone.utc)
pdf_bytes = typst.compile("hello.typ", timestamp=timestamp)
# Also for svg
svg_bytes = typst.compile("hello.typ", format="svg")
# Typst 0.15 minifies PDF, SVG, and HTML by default; pass pretty=True for
# human-readable export output.
svg_bytes = typst.compile("hello.typ", format="svg", pretty=True)
# Multiple compatible PDF standards can be requested at once.
pdf_bytes = typst.compile("hello.typ", pdf_standards=["a-2a", "ua-1"])
# Skip PDF accessibility tags for smaller files, like the CLI's --no-pdf-tags.
# Tags cannot be disabled for PDF/A-1a, PDF/A-2a, PDF/A-3a, and PDF/UA-1.
pdf_bytes = typst.compile("hello.typ", pdf_tags=False)
# For multi-page export (the template is the same as the typst cli)
images = typst.compile("hello.typ", output="hello{n}.png", format="png")
# Or use Compiler class to avoid reinitialization
compiler = typst.Compiler()
compiler.compile(input="hello.typ", format="png", ppi=144.0)
# Query something
import json
values = json.loads(typst.query("hello.typ", "<note>", field="value", one=True))
# Or use Typst's newer eval-style query expression
values = json.loads(typst.eval("hello.typ", "query(<note>).first().value"))
Watch mode
Compiler.watch recompiles whenever the input or any file it depends
on (imports, images, data files) changes, like typst watch. Keep the output open in a
viewer next to your editor: