Skip to Content
prasasti

Formats

What prasasti reads today, how it tells formats apart, and what's planned.

prasasti.Document("report.pdf").format   # "pdf"
prasasti.Document("notes.txt").format    # "txt"

prasasti never trusts the file name. It looks at the first bytes: a %PDF- marker in the first kilobyte means PDF, bytes that decode as text mean plain text, and anything else fails with an unsupported error.

PDF

  • Text of every page in reading order. Reading order explains how.
  • Page count without reading any page. Opening a 60,000-page file and counting its pages reads the cross-reference table and one dictionary.
  • Document info such as Title, Author and Producer, through metadata.
  • Encrypted files: RC4 (40 to 128 bit), AES-128 and AES-256. The password is tried as the user password, then as the owner password.
  • Damaged files: a broken or missing cross-reference table, or a file cut off partway. prasasti rebuilds the structure by scanning for objects, and was_recovered tells you it did.
  • Pages read in parallel with threads=0.

Scanned pages that are only images have no text to extract, so they come back as empty strings. prasasti doesn't run OCR.

Plain text

  • UTF-8, with or without a byte order mark.
  • UTF-16, little or big endian, with a byte order mark or detected from the byte pattern.
  • Windows-1252 for older 8-bit files that aren't valid UTF-8.
  • A form feed (\f) splits the file into pages, the way pdftotext writes them. A file without one is a single page.
  • metadata holds the detected encoding: Utf8, Utf16Le, Utf16Be or Windows1252.

Planned

DOCX and HTML come next, then converting between formats. A new format arrives behind the same Document API, so code that opens a PDF today opens a DOCX the same way.

On this page