Sub topic 4
Pull the Text Out of the PDF
Assume you can run basic Python code and install a package with pip. This chapter uses pypdf, a Python library for reading PDF files. Text extraction is a discovery step. It helps you find candidate facts and messages before you write carousel slides. It does not replace reading the source visually.
Check the PDF Before Extracting
PDFs store text by page and by position. A file may contain selectable text, scanned images, charts, or a mixture of all three.
Open the file in a PDF viewer and check these questions:
- Can you select and copy a sentence?
- Does the text copy in the expected reading order?
- Are headings, columns, footnotes, and tables understandable?
- Are important findings shown only as images?
- Does the PDF contain a source date, author, or organization name?
If you cannot select meaningful text, the document may require optical character recognition (OCR). OCR is software that turns text in an image into machine-readable characters. OCR can introduce errors, especially with numbers and small labels.
Extract Page Text with Python
Install pypdf once in your environment:
pip install pypdfThe following script prints a short preview from every page. The preview makes it easier to scan a long report without manually copying each page.
from pathlib import Path
from pypdf import PdfReader
pdf_path = Path("report.pdf")
if not pdf_path.exists():
raise FileNotFoundError(f"Could not find {pdf_path}")
reader = PdfReader(str(pdf_path))
print(f"Pages: {len(reader.pages)}")
for number, page in enumerate(reader.pages, start=1):
text = page.extract_text() or ""
cleaned = " ".join(text.split())
print(f"\n--- Page {number} ---")
print(cleaned[:500] or "[No selectable text found]")extract_text() returns the text that the PDF exposes. The expression or "" handles pages where no text is available. " ".join(text.split()) removes repeated spaces and line breaks so the preview is easier to read.
For closer analysis, save all extracted text with page markers:
from pypdf import PdfReader
reader = PdfReader("report.pdf")
with open("report-text.txt", "w", encoding="utf-8") as output:
for number, page in enumerate(reader.pages, start=1):
text = page.extract_text() or ""
output.write(f"\n--- Page {number} ---\n")
output.write(text)
output.write("\n")Page markers preserve a path back to the original source. Record the page number for every statistic or claim you may use in the carousel.
Validate the Extracted Text
Extraction can silently damage meaning. Look for:
- Numbers split across lines, such as
2followed by5% - Missing minus signs or decimal points
- Words read in the wrong order from columns
- Repeated headers and footers
- Table rows merged into one sentence
- Characters confused by OCR, such as
0andO
Compare important claims against the rendered PDF. Treat extracted text as a search aid, not as unquestioned evidence.
Exercise
A page preview shows “25%” as “2 5%,” and the report uses two columns. What should you do before using that statistic?
Show answer
Open the rendered page, verify the number and its context, then record the corrected value with the page number. Do not rely on the extracted string alone.