Skip to Content
prasasti

Python

Every class, method and function in the prasasti Python package, with the errors each one raises.

import prasasti

doc = prasasti.Document("report.pdf")
pages = doc.extract_text(threads=0)

Install with pip install prasasti. Wheels ship for Linux (x86_64, aarch64), macOS (Intel, Apple silicon) and Windows (x64). Type hints are included.

Document

prasasti.Document(source, password=None)

Opens a document and detects its format from the content.

  • source: a path (str or os.PathLike), or the file's contents as bytes or bytearray.
  • password: the user or owner password of an encrypted PDF. Leave it out when the file has an empty user password.
PropertyTypeWhat it holds
formatstr"pdf" or "txt"
page_countintNumber of pages. len(doc) gives the same number.
is_encryptedboolTrue when the file was encrypted and opened with the password
was_recoveredboolTrue when the file was damaged and prasasti rebuilt its structure
metadatadict[str, str]PDF document info (Title, Author, ...). For a text file, {"Encoding": "Utf8"} and so on.
MethodReturnsWhat it does
extract_text(threads=1)list[str]The text of every page, in reading order
text(threads=1)strEvery page joined by a form feed (\f)
page_text(index)strThe text of one page, counting from 0

threads=0 uses every core. Any other number uses that many threads.

Functions

For a quick one-off, these open the file, do one thing and close it.

text = prasasti.extract_text("report.pdf", password=None, threads=1)  # str, pages joined by \f
pages = prasasti.page_count("report.pdf", password=None)              # int
print(prasasti.__version__)                                          # "0.1.0"

Both take the same source as Document, so bytes work too.

Threads and the GIL

prasasti releases the GIL while it opens files and extracts text. Two Python threads working on two documents run at the same time, and an async app can hand the work to asyncio.to_thread without blocking the event loop.

import asyncio
import prasasti

async def read(path):
    return await asyncio.to_thread(prasasti.extract_text, path)

Errors

ExceptionWhen
OSErrorThe file can't be read
ValueErrorThe file can't be parsed, the password is wrong, or the format isn't supported
IndexErrorpage_text got a page number past the last page
TypeErrorsource is neither a path nor bytes
try:
    doc = prasasti.Document("locked.pdf")
except ValueError as e:
    print(e)  # encrypted: ...

On this page