Skip to main content

Convert pandas DataFrames into publication-quality LaTeX/PDF tables

Project description

bambootex

Convert pandas DataFrames into publication-quality LaTeX/PDF tables using tabularray.

Requirements

  • Python >= 3.10
  • A TeX distribution with pdflatex (e.g. TeX Live)
  • The following LaTeX packages: tabularray, siunitx, xcolor, babel

Installation

pip install bambootex

Quick start

import pandas as pd
from bambootex import Table, Cell, TextFormatter, GradientHighlighter, SimpleHighlighter

df = pd.read_csv("iris.csv")

tbl = Table(
    df,
    columns=["species", "sepal_length", "sepal_width"],
    column_formatters={
        "species": TextFormatter(font=r"\textsf", size=r"\small"),
    },
    headers=[
        [Cell("Species", vspan=2), Cell("Sepal", hspan=2)],
        [Cell(""), Cell("Length"), Cell("Width")],
    ],
    packages=["libertine"],
)

tbl.sort_by("sepal_length")
tbl.highlight("sepal_length", GradientHighlighter("white", "red"))
tbl.to_pdf("output.pdf")

API

Table

Table(
    df,                     # pd.DataFrame
    columns,                # columns to include, in order
    column_formatters=None, # dict of column -> format string or callable
    headers=None,           # list of header rows, each a list of Cell
    packages=None,          # extra LaTeX packages to load
    number_format=".2f",    # default format for float columns
)

.sort_by(key, reverse=False)

Sort rows before rendering. key can be:

  • a column name: tbl.sort_by("value")
  • a list of column names: tbl.sort_by(["group", "value"])
  • a callable applied row-wise: tbl.sort_by(lambda r: r["a"] - r["b"])

.highlight(column, highlighter, *fns)

Highlight cells in column using highlighter. Optional filter functions fns are ANDed together to select which rows to highlight — omit them to highlight all rows.

# All rows, gradient from white to red
tbl.highlight("score", GradientHighlighter("white", "red"))

# Rows matching a condition
tbl.highlight("score", SimpleHighlighter("yellow"), lambda x: x > 90)

# Multiple conditions (ANDed)
tbl.highlight("score", SimpleHighlighter("green"), lambda x: x > 50, lambda x: x < 80)

.to_tex(output_path) / .to_pdf(output_path)

Render to a .tex file or compile directly to PDF (requires pdflatex on $PATH).


Cell

Defines a header cell, optionally spanning multiple columns or rows.

Cell(
    text,        # cell content
    hspan=1,     # columns to span
    vspan=1,     # rows to span
    align="c",   # tabularray halign: "l", "c", or "r"
)

TextFormatter

Applies LaTeX font commands to a column's values.

TextFormatter(font=r"\textsf", size=r"\small")
# renders each value as: \small\textsf{value}

Highlighters

Class Args Effect
SimpleHighlighter color Fills matching cells with a flat xcolor color
GradientHighlighter color_min, color_max Interpolates between two colors based on the cell's relative value

Colors are plain xcolor named colors ("red", "white", "blue!30", etc.).
GradientHighlighter colors must be base named colors without ! mixing, as they are used inside the gradient formula internally.

Custom highlighters

Any callable matching (series: pd.Series) -> dict[index, color_str] works as a highlighter:

def my_highlighter(series):
    return {idx: "green" if v > 0 else "red" for idx, v in series.items()}

tbl.highlight("delta", my_highlighter)

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

bambootex-0.1.0.tar.gz (146.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bambootex-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file bambootex-0.1.0.tar.gz.

File metadata

  • Download URL: bambootex-0.1.0.tar.gz
  • Upload date:
  • Size: 146.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for bambootex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e1e0b931780679dc6948dcaf8f68b1e17dad8cff19b4e77bd6e2a8c9fa0b6b14
MD5 6f80927b9bf868fb48e4662c0a7616ce
BLAKE2b-256 209f5bad7c6055cfa08f2c020c766e6231cab3db302e4b343107df3a9a5c330c

See more details on using hashes here.

File details

Details for the file bambootex-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bambootex-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for bambootex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 90e6c79b6d68d28683400222b5569a7edf723c9a978998a8762ec338dbe72d38
MD5 be132ef411f37dae1d17e8962c660f4a
BLAKE2b-256 80de188ae737e7c34d3c3a4cb19622433b14f00b123208283a139da0a7cdf40d

See more details on using hashes here.

Supported by

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