Skip to content

pyaccesskit.units

pyaccesskit.units

Physical lengths for form and report layout.

Access measures positions and sizes in twips (1/1440 inch, 1/20 point). PyAccessKit lets you work in familiar units instead and converts exactly once, when talking to Access::

from pyaccesskit.units import cm, mm, inch

width = cm(6) + mm(5)       # Length(twips=3685)
width.cm                    # 6.5 (approximately; lengths are whole twips)

A :class:Length is an immutable whole number of twips. In specs it serializes to a readable string such as "6.5cm" and parses from strings ("2cm", "10mm", "1.5in", "12pt", "300tw") or plain integers (twips).

Length

Length(twips: int)

An immutable length, stored as a whole number of twips.

Construct lengths with the helper functions :func:twips, :func:pt, :func:mm, :func:cm and :func:inch, or parse text with :meth:parse.

twips property

twips: int

The length in twips (the unit Access uses).

points property

points: float

The length in typographic points.

mm property

mm: float

The length in millimetres.

cm property

cm: float

The length in centimetres.

inches property

inches: float

The length in inches.

parse classmethod

parse(value: str | int | Length) -> Length

Parse "2cm", "10 mm", "1.5in", "12pt", "300tw" or an integer number of twips.

Raises:

Type Description
ValueError

If the text is not a recognised length.

format

format(unit: str = 'cm') -> str

Format in unit ("cm", "mm", "in", "pt" or "tw").

The result is the shortest decimal that parses back to exactly the same number of twips: cm(1.5).format() == "1.5cm" and Length.parse(x.format(unit)) == x for every length and unit.

twips

twips(value: int) -> Length

A length of value twips (1/1440 inch).

pt

pt(value: float) -> Length

A length of value typographic points (20 twips each).

mm

mm(value: float) -> Length

A length of value millimetres.

cm

cm(value: float) -> Length

A length of value centimetres.

inch

inch(value: float) -> Length

A length of value inches.