Skip to main content

Pypst

Declarative Typst in Python with Pandas data frame support.

Pypst helps you dynamically generate Typst code directly in Python. No manual string manipulation required. It has two major use cases:

  • Generating full Typst documents to be rendered as PDFs
  • Generating and styling Typst tables from Pandas data frames to be included in another Typst document

Pypst produces human-readable Typst code that you can modify and extend. It will not compile Typst code for you, but you can either use the Typst compiler to do so in the command line or install the Typst Python package.

Example script and output

Installation

Pypst is available on PyPI and can be installed via pip.

pip install pypst

To use the table functionality, pandas is needed. Install Pypsts like this instead:

pip install pypst[pandas]

Usage

Pypst contains classes that represent Typst elements. You can create a Typst document by instantiating these classes and nesting them as needed.

>>> import pypst
>>> heading = pypst.Heading("My Heading", level=1)
>>> itemize = pypst.Itemize(["First item", "Second item", "Third item"])

Each of these classes has a render method that returns the Typst code as a string.

>>> print(heading.render())
= My Heading
>>> print(itemize.render())
- First item
- Second item
- Third item

If you want to combine multiple elements into a single document, you can use the Document class. You can even add imports for other Typst files or packages.

>>> document = pypst.Document([heading, itemize])
>>> document.add_import("utils.typ")
>>> print(document.render())
#import "utils.typ"
<BLANKLINE>
= My Heading
<BLANKLINE>
- First item
- Second item
- Third item

The output of the render method can be written to a .typ file for compilation. The documentation contains more extensive tutorials on generating documents and generating tables from Pandas data frames.

Limitations

Pypst is not a Typst compiler. It doesn't check for syntax errors or compile Typst code into PDFs. Pypst interprets any string as a Typst literal, so stroke = "1pt" will be rendered as stroke: 1pt. If you want to pass a string literal, you need to wrap it in quotes, for example, caption = '"My Caption"'. When Pypst encounters a string wrapped in quotes without need, it will automatically remove them. For a content block, you can wrap the string in brackets instead, for example, caption = "[My content]".

Roadmap

If there is time and people are interested, I would like to add the following features:

  • Complete table attributes (for example, fill is missing)
  • Support automatic formating for common workflows, like automatically merging multi-level columns with mean and standard deviation
  • Add more Typst elements (like headings or lists) to make building more complex documents easier

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Metadata

Release files for pypst 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pypst 0.8.1
File Size Uploaded
pypst-0.8.1.tar.gz 19.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pypst 0.8.1
File Interpreter ABI Platform
pypst-0.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 41.8 kB

Release files / pypst-0.8.1.tar.gz

Download URL pypst-0.8.1.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b7c425250cc1d1317df1bf453fcd14ef14a7801af4a79650c123f10f29276cf6
BLAKE2b-256 checksum
How to use checksums
6281e6edba64385b5ecbf93282c4c883d61a7fe26aacd964517aeb927e9fe8fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.3 Linux/6.17.0-1022-azure

Release files / pypst-0.8.1-py3-none-any.whl

Download URL pypst-0.8.1-py3-none-any.whl
Size 22.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5f182388db1dc34e8d7447cc6090801adbacd6732f3694fd4fc17a7b65358731
BLAKE2b-256 checksum
How to use checksums
ce238f095abfdca008ebb10d7908bb4a7ef077bdb6862d582ee2d8d5b925b983
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.12.3 Linux/6.17.0-1022-azure

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page