A minimal Python wrapper around the PAGE-XML format for OCR output. Can also import ALTO-XML.
pip install pygexml
Requires Python 3.12+.
from pygexml import Page
page = Page.from_xml_file("docs/xml_file.xml")
for line in page.all_text():
print(line)All dataclasses are serializable with to_dict/from_dict and to_json/from_json via dataclasses-json.
| Class | Import from |
|---|---|
Page |
pygexml |
Page, TextRegion, TextLine, Coords, Label |
pygexml.page |
Point, Box, Polygon |
pygexml.geometry |
Page,TextRegionandTextLineeach exposeall_text()andall_words()iterators.- On
Page, these respect the PAGE-XML reading order if present. - Lookups by ID are available via
lookup_region()andlookup_textline(). - The reading order is also accessible directly via
regions_ordered(). TextLine.confidenceis read from PAGE-XMLTextEquiv/@conf. ALTO confidence is not mapped because itsString/@WCvalues are defined per word, below the granularity of this model.Page.labelsandTextRegion.labelscontain semantic PAGE-XML labels asset[Label]. EachLabelhas a requiredvalueand optionaltypeandcomments.
Refer to the online API docs for details.
The pygexml.strategies module provides Hypothesis strategies for all pygexml types, ready to use in property-based tests - including downstream projects:
from hypothesis import given
from pygexml.strategies import st_pages
@given(st_pages())
def test_my_page_processing(page):
assert process(page) is not NoneRefer to the pygexml.strategies API docs for details.
pip install ".[dev,test,docs]"
black pygexml test # format
mypy pygexml test # type check
pyright pygexml test # type check
pytest -v # tests
pdoc -o .api_docs ./pygexml # API docsCI runs on Python 3.12, 3.13 and 3.14. API documentation is published to GitHub Pages on every push to main.
Bug reports, feature requests and pull requests are welcome. Feel free to open draft pull requests early to invite discussion and collaboration.
Please note that this project has a Code of Conduct.
Copyright (c) 2026 Mirko Westermeier, Katharina Dietz (SCDH, University of Münster)
Released under the MIT License.