Skip to content

Read page by page

extract_text returns the whole document in one string, but you can still work with each page.

ozaru separates pages with a form feed character, written \f in Python. Split on it to get a list with one item per page:

import ozaru

text = ozaru.extract_text("report.pdf")
pages = text.split("\f")

for number, page in enumerate(pages, start=1):
    title = page.splitlines()[0]
    print(f"Page {number}: {title}")

Output:

Page 1: Quarterly Report
Page 2: Sales
Page 3: Next Steps

Use split, not splitlines

splitlines() also treats \f as a line break, so it can't tell you where a page ends. Use split("\f") to get pages, and splitlines() on each page to get its lines.

Next: handle errors.