Skip to main content

Convert pandas DataFrames into LaTeX/PDF tables

Project description

bambootex

Convert pandas DataFrames into LaTeX/PDF tables using tabularray.

Requirements

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

Installation

pip install bambootex

Quick start

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

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

tbl = Table(
    df,
    columns=["species", "sepal_length", "sepal_width"],
    column_formatters={
        "species": TextFormatter(font=Font.sf, size=Size.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, NumberFormatter, or TextFormatter
    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 (str or NumberFormatter)
    vlines=None,             # list of column indices to draw a vertical line after
    hlines=None,             # list of row indices to draw a horizontal line after
    default_size=None,       # document-wide font size (str or Size)
)

.sort_by(key, reverse=False)

Sort rows before rendering. key can be a column name, list of column names, or a row-wise callable.

.highlight(column, highlighter, *fns)

Highlight cells in one or more columns.

  • Single column (column: str): filter functions receive the full column series.
  • Multiple columns (column: list[str]): a single predicate (value, row_subset) -> bool selects which cells to highlight per row.
# Gradient across all rows
tbl.highlight("score", GradientHighlighter("white", "red"))

# Highlight minimum per row across columns
tbl.highlight(
    ["time_a", "time_b", "time_c"],
    SimpleHighlighter("green!25"),
    lambda v, row: v == row.min(),
)

.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.

Cell(
    text,        # cell content
    hspan=1,     # columns to span
    vspan=1,     # rows to span
    align="c",   # tabularray halign: "l", "c", or "r"
    font=None,   # LaTeX font command, e.g. Font.tt or r"\texttt"
)

TextFormatter

Applies LaTeX styling to a column's string values. When align is set, it also controls the column's alignment in the colspec.

TextFormatter(
    font=Font.sf,       # font command or list of font commands
    size=Size.small,    # size command
    align="c",          # column alignment ("l", "c", "r")
    prefix="(",         # prepended to each value
    suffix=")",         # appended to each value
)

NumberFormatter

Controls formatting of numeric columns.

NumberFormatter(
    decimal_places=2,   # decimal places
    unit=None,          # unit string appended after value
    nan=None,           # string to substitute for NaN values (e.g. "--")
)

Font

Font.rm   # \textrm
Font.sf   # \textsf
Font.tt   # \texttt
Font.bf   # \textbf
Font.it   # \textit

Size

Size.tiny / Size.scriptsize / Size.footnotesize / Size.small / Size.normalsize
Size.large / Size.Large / Size.LARGE / Size.huge / Size.Huge

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 relative value

Any callable matching (series: pd.Series) -> dict[index, color_str] also works as a 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.1.tar.gz (136.9 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.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for bambootex-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3fa5d06b0feb2e0c028759ba87e2850d95c32f5b93a689ab561fb18b3cf21cff
MD5 5244f83284099ce7e48d3ed3a7659471
BLAKE2b-256 bd2ac953f934c6f8268e5c4855b8fa8568aa1dc19687793ffb9e6d1999564d44

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for bambootex-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b39e6c9d6b45f093c68c9050a48069be55f36a5ea1a83f759e20a536fe748c12
MD5 94b046bf156dda8be01d337e33d7c479
BLAKE2b-256 ddf86664c26bc137f0657be6856aed4f0fa6f4858bfe676aa84a7b551124682a

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