──────────────────────────────────────

54. Work with Spreadsheets, PDFs and Images · Read PDF Pages

Number PDF pages and flag pages whose extracted text needs a closer look.

Lesson files · lessons/documents-and-images/read-pdf-pages

Before you start
- Number entries with enumerate() starting at 1.
- Use strip() to detect a string that contains only spaces.
- Read a list of text values from JSON.

By the end
- Use the extracted text from each page to build a numbered preview.
- Explain why a scanned page can look readable and give an empty result.
- Test a PDF preview with an empty page between two pages of text.

Where you can use it
Check a club’s welcome pack before sharing it. Flag pages whose text preview is empty and open them to see which ones need a closer look.

PDF preserves the visual layout of pages. A file saved from a word processor may carry embedded text that pypdf can extract page by page. A scanned PDF may store each page as a picture. Reading letters from that picture requires optical character recognition, or OCR. An empty extraction could also come from an intentionally blank page. Flag either case for a person to inspect.

Studio supplies a list of strings that stand for what pypdf returned from each page. Trim each string before checking for empty text. Use enumerate() to keep the original page numbers. In the local lab, open the actual PDFs with PdfReader and call extract_text() on each page. Then use PdfWriter to copy the pages into a new print pack in the supplied order.

Predict
What will the program do with the second page?
- Remove the page from the report.
- Keep its number and print Review.

Worked example (example.py)

page_texts = ["  Equipment list ", "   "]
for number, text in enumerate(page_texts, start=1):
    cleaned = text.strip()
    if cleaned == "":
        cleaned = "Review"
    print(f"Page {number} - {cleaned}")


Example result
Page 1 - Equipment list
Page 2 - Review

Read a JSON list of extracted page texts. For each page, print Page 1 - followed by the trimmed text or Review. Replace 1 with the actual page number. Finish with Review pages: and the number of pages with empty text.

Starter program (starter.py)

import json

page_texts = json.loads(input())
for number, text in enumerate(page_texts, start=1):
    print(f"Page {number} - {text.strip()}")


Starter input
[" Club Kit Schedule ","   ","Meet outside the lab"]

Hint
Clean the page text before checking it. Add to the review count only when the cleaned string is empty.

Case 1. Try the sample
Input
[" Club Kit Schedule ","   ","Meet outside the lab"]
Expected output
Page 1 - Club Kit Schedule
Page 2 - Review
Page 3 - Meet outside the lab
Review pages: 1

Case 2. One text page
Input
["Kit list"]
Expected output
Page 1 - Kit list
Review pages: 0

Case 3. Two pages to check
Input
["","  "]
Expected output
Page 1 - Review
Page 2 - Review
Review pages: 2

Stretch
Try flagging pages that contain a chosen word as well. Keep that count separate from the empty-text count.

Reflect
What would you check in the actual PDF before deciding why a page has empty extracted text?

Check your prediction
Keep its number and print Review. Every page keeps its number. A string that is empty after strip() gets the review label.
