Skip to content

Extract text

ozaru.extract_text returns the text of the whole document as a single str.

All the examples in this tutorial use the sample report.

Search the text

The result is a normal Python string, so you can use everything you already know. For example, in tells you whether a word appears in the document:

import ozaru

text = ozaru.extract_text("report.pdf")

print("São Paulo" in text)
print("London" in text)

Output:

True
False

Accents and other non-English characters are preserved, so São Paulo is found exactly as it was written.

Save the text to a file

To keep the text, write it to a file:

from pathlib import Path

import ozaru

text = ozaru.extract_text("report.pdf")
Path("report.txt").write_text(text, encoding="utf-8")

print("Saved report.txt")

Output:

Saved report.txt

Tip

Always pass encoding="utf-8" when you save text. Otherwise Python uses the default encoding of your system, and some characters may fail to save.

Scanned documents

ozaru reads the text stored inside the PDF. A scanned document is really a set of images, so it may return little or no text. See Limitations.

Next: count pages.