Python 绑定到 typst
Python binding to typst, a new markup-based typesetting system that is powerful and easy to learn.
pip install typst…You can compile multi-file Typst projects by passing a dictionary mapping filenames to
their content as bytes. The entry point must be keyed as "main" or "main.typ" (if
there is only one file, any key works):
import typst
files = {
"main.typ": b'#import "lib.typ": greet\n= Hello\n#greet("World")',
"lib.typ": b'#let greet(name) = [Hello, #name!]',
}
pdf = typst.compile(files, format="pdf")This is useful when Typst sources are bundled as Python package resources:
import typst
import importlib.resources
files = {}
for filename in ["main.typ", "lib.typ", "utils.typ"]:
files[filename] = importlib.resources.read_binary("mypackage.typst_files", filename)
pdf = typst.compile(files, format="pdf")Dictionary values can also be file paths (as strings or Path objects), which will be
read from disk.
Use package_path to point Typst at a local package directory and
package_cache_path to control where downloaded @preview packages are cached:
import typst
pdf = typst.compile(
"main.typ",
package_path="vendor",
package_cache_path="vendor",
)You can pass values to the compiled Typst file with the sys_inputs argument. For example:
import json
import typst
persons = [{"name": "John", "age": 35}, {"name": "Xoliswa", "age": 45}]
sys_inputs = {"persons": json.dumps(persons)}
typst.compile(input="main.typ", output="ages.pdf", sys_inputs=sys_inputs)The following example shows how the passed data can be used in a Typst file.
#let persons = json(bytes(sys.inputs.persons))
#for person in persons [
#person.name is #person.age years old. \
]This work is released under the Apache-2.0 license. A copy of the license is provided in the LICENSE file.
暂无开放 Issues,或尚未同步最近议题。