Skip to main content

Declarative Typst in Python with Pandas data frame support.

Project description

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypst-0.2.0.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

pypst-0.2.0-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file pypst-0.2.0.tar.gz.

File metadata

  • Download URL: pypst-0.2.0.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for pypst-0.2.0.tar.gz
Algorithm Hash digest
SHA256 440f8cdaad39eee31314032201f8ac8c5297d40169815978e2ead84e81efec67
MD5 e3a43d64b61dba8b52c25d0872271ca9
BLAKE2b-256 f08161a33b9dd02c97ff153f733f316868d1be051958d1a442251a4734aec662

See more details on using hashes here.

File details

Details for the file pypst-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pypst-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for pypst-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6f939139eca1c8ecec86535b6202bf58a998904b23ac02ba66c3003d9baf732
MD5 5af48fcb63d1f569cbf7b7e7c0120df8
BLAKE2b-256 2be6e16aa61922407c67be45732655b9c05b0961995e504bea0e7a241c2554a8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page