Sub topic 4
Pull the text out of the PDF
This chapter covers the step between reading your source and choosing a message: getting the text out of the PDF so you can search it, quote it accurately, and copy statistics without retyping them. It assumes you have a source PDF, you have already named your goal and reader, and you can run a short Python script. The Python parts are optional. You can do the same job by hand in any PDF viewer.
Why extract text at all
Reading a 40-page report on screen works for understanding. It works poorly for carousel writing, for three reasons.
- You need exact numbers and quotes. Retyping from a screen invites errors, and a wrong statistic on a public post is hard to take back.
- You need to scan fast. Search and skim across all pages show you which pages carry findings and which carry method notes.
- You need a plain-text record. A text file of the source lets you check every slide claim against the original later.
Extraction is a tool for finding and checking material. It does not choose your message for you. That decision comes in the next chapter.
What a PDF actually stores
A PDF is a layout format, not a text format. It records where to draw each piece of text on a page, not how the text flows as sentences. Extraction tools rebuild the reading order by guessing from those positions.
This explains the common errors:
- Two-column pages may come out with lines from both columns interleaved.
- Headers, footers, and page numbers appear in the middle of paragraphs.
- Hyphenated words at line ends stay split, such as
consol-followed byidated. - Tables turn into strings of numbers with no clear row and column boundaries.
- Charts contribute only their labels, and often not even those.
Extract text with pypdf
The pypdf library reads a PDF page by page. Install it first.
pip install pypdfThe script below prints the first 500 characters from each page. That is enough to see what each page is about without flooding your terminal.
from pypdf import PdfReader
reader = PdfReader("report.pdf")
for number, page in enumerate(reader.pages, start=1):
text = page.extract_text() or ""
print(f"--- Page {number} ---")
print(text.strip()[:500])Three details matter here.
enumerate(..., start=1)numbers pages the way a reader sees them, so your notes match the PDF viewer.extract_text()can return an empty string orNoneon pages without a text layer. Theor ""guard prevents a crash on.strip().- Slicing with
[:500]is for scanning only. Do not use the truncated output as your quote source.
Save the full text for searching
Once the preview shows the PDF has a usable text layer, save everything to a file with page markers. The page markers let you cite the page for each statistic.
from pypdf import PdfReader
reader = PdfReader("report.pdf")
with open("report.txt", "w", encoding="utf-8") as out:
for number, page in enumerate(reader.pages, start=1):
text = page.extract_text() or ""
out.write(f"\n=== Page {number} ===\n")
out.write(text.strip() + "\n")Open report.txt in any editor and search for words from your audience's vocabulary, such as "increase", "percent", "respondents", or "recommend". Candidate findings cluster around those words.
A workflow for scanning
The diagram shows the loop that works well in practice.
The last step is the one people skip. Always compare each number you plan to use with the original page, because extraction can drop a decimal point, merge two columns, or attach a figure to the wrong label.
Clean up the common problems
Light cleanup improves what you read. Keep your cleanup small and visible, so you do not accidentally change the meaning.
| Problem | Symptom | Fix |
|---|---|---|
| Split hyphenated words | `consol-` then `idated` | Join by hand when you quote, or replace `-\n` with an empty string |
| Repeated headers and footers | Report title appears every page | Ignore them, or filter lines you recognize |
| Interleaved columns | Sentences switch topics mid-line | Read from the PDF viewer for that page |
| Broken tables | Numbers with no labels | Copy the values from the viewer and check the row and column |
| Empty pages | `extract_text()` returns nothing | Scanned page, so read by hand or use OCR |
A small helper handles the hyphen case and collapses stray line breaks:
import re
def tidy(text: str) -> str:
text = re.sub(r"-\n(\w)", r"\1", text) # join words split across lines
text = re.sub(r"\s*\n\s*", " ", text) # turn line breaks into spaces
return re.sub(r"\s{2,}", " ", text).strip()
sample = "Most respondents consol-\nidated their\ntoolset this year."
print(tidy(sample))This prints Most respondents consolidated their toolset this year.
The hyphen rule has an edge case. It also joins genuine hyphenated compounds that happen to break at a line end, such as well-\nknown, into wellknown. Check any word you plan to quote.
When there is no text layer
If a page returns nothing, you have three options.
- Read and type the few figures you need by hand, and double-check them.
- Look for a text version of the same report, such as a web page or an accompanying data file. Publishers often provide one.
- Run optical character recognition (OCR), which converts images of text into text. Tools such as Tesseract do this. OCR makes mistakes with similar characters like
1,l, and0,O, so verify every number.
For a carousel you rarely need the whole document. You need five to ten solid facts, so by hand is often the fastest route.
Know the limits
Extraction gives you words and numbers. It does not give you the meaning of a chart, the caveat in a footnote, or the context of a quote. When a statistic looks striking, find the sentence around it and the method note that explains who was surveyed. A figure that holds for "enterprise teams" should not become "teams" on your slide.
Also note the page and section for each fact you extract. The rights chapter that follows depends on knowing exactly which material you used, and the caption chapter will need accurate attribution.
Practice
Take a PDF you plan to use. Run the preview script, then save the full text. Pick three candidate statistics, record the page number for each, and verify each against the PDF viewer. Write down one fact you could not extract cleanly and say how you handled it.
Exercise
A page of your source report returns an empty string from extract_text(), but you can see text on it in your PDF viewer. What is the likely cause, and what are two ways to get the figures you need?
Show answer
The page is probably a scanned image with no text layer, so there is nothing for pypdf to read. You can read and type the needed figures by hand and double-check them, or run OCR (for example with Tesseract) and verify every number, since OCR confuses similar characters. Looking for a text version of the report is another option.
Exercise
Your extracted text shows "62% of teams cut tools" on page 14. Before putting this on a slide, what two checks should you make?
Show answer
Compare the number with the original page 14 in the PDF viewer, because extraction can drop or misplace digits and labels. Then read the surrounding sentence and method note to confirm who the 62% describes (such as "survey respondents at enterprise companies") and that the unit is a percent of that group, so the slide does not overstate the claim.