🚀 PDF to Audiobook
A small Python program that reads a PDF file and turns its text into speech, so you get a free audiobook. It is the Day 91 assignment of the Udemy course 100 Days of Code: The Complete Python Pro Bootcamp, for course participants and anyone who browses the repository for ideas.
📚 Table of Contents
🧭 Overview
The program extracts the text of a PDF with pypdf, splits it into pieces of at most 400 characters and speaks each piece with Coqui TTS, an open source speech engine that runs on your own machine. No paid service and no account are needed. The assignment's function is convert_pdf_to_audiobook.
| Folder | What it holds |
|---|---|
src/pdf_to_audiobook/ |
The program, in layers: domain, application, infrastructure, presentation. All constants are in constants.py |
tests/ |
pytest tests |
docs/ |
Business case, plan, milestones, reviews, images and the wiki sources (docs/wiki/) |
scripts/ |
The opengrep security scan and the window test in a container |
docker/ |
The image and the script of the window test |
tools/ |
capture_step.py, which renders command output as an SVG image for the documentation |
The project is built in seven lecture steps. Each step has a screenshot below and a page in the wiki (English, Danish and Bahasa Malaysia) that explains what we gained.
Step 1: Environment
Step 2: Read the PDF
Step 3: Speak with Coqui TTS
Step 4: Convert and command line
Step 5: Tests and quality
Step 6: The speak text window
Step 7: Open a PDF in the window
📋 Requirements
- Python 3.13 or newer, because the project targets it.
git, to clone the repository.- About 2 GB of free disk space and a few minutes: the project installs
pypdf, Coqui TTS, PyTorch andsounddevice, and downloads a voice model (about 110 MB) on the first run. The speech engine runs on your own machine; no account and no paid service are needed. - Optional for development: opengrep (
~/.opengrep/cli/latest/opengrep.exe) for the security scan, and Doxygen for the API documentation.
🛠️ Setup
Create a local virtual environment, upgrade pip and install the project with the development tools.
Windows PowerShell:
git clone https://git.tirsystem.com/Tirsvad-Udemy-100-days-of-code/091-assignment-convert-pdf-to-audiobook.git
cd 091-assignment-convert-pdf-to-audiobook
python -m venv .venv
.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
Linux and macOS:
git clone https://git.tirsystem.com/Tirsvad-Udemy-100-days-of-code/091-assignment-convert-pdf-to-audiobook.git
cd 091-assignment-convert-pdf-to-audiobook
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -e ".[dev]"
The folder .venv/ is ignored by git. Leave the environment with deactivate.
▶️ Run
With the virtual environment active, convert the sample PDF into a WAV file:
python -m pdf_to_audiobook tests/data/sample.pdf -o build/sample.wav
The same program is installed as a command:
pdf-to-audiobook my-book.pdf
pdf-to-audiobook my-book.pdf -o audio/my-book.wav --model tts_models/en/ljspeech/tacotron2-DCA
The speech library prints a lot of text while it works; the program hides it. Add --verbose to see it again, for example when you diagnose a problem. This works for the command and for the window.
Without -o the audio is written next to the PDF with the suffix .wav. Exit code 0 means success, 1 an error (the message is on stderr) and 2 a wrong command line.
In Python the assignment's function is available as convert_pdf_to_audiobook(pdf_path, output_path, engine, extract_text); see the wiki for the design.
Speak typed text or a PDF
Open a window, type text and press Speak, or press Open PDF to pick a PDF file, and hear it through the speakers. Stop ends the speech:
pdf-to-audiobook-window
While the voice model loads, the status line says Loading voice.... It uses the same voice model as the converter and plays the sound with sounddevice. On Linux install the system library libportaudio2 first.
🧪 Tests
With the virtual environment active:
python -m pytest
The real speech engine is tested by one slow test that is skipped by default and downloads the voice model on first use:
python -m pytest -m slow
Check the code style and the types:
python -m ruff check src tests
python -m mypy
The window is tested in a Linux container on a virtual display, never on your desktop (Docker is needed). The log and a screenshot go to build/gui-results:
pwsh scripts/gui-test.ps1
Run the security scan before every push:
pwsh scripts/scan.ps1
📄 License
GNU Affero General Public License v3, see LICENSE.
🔗 Links
📎 Appendix: Project documents
The documents a newcomer needs, in reading order.
| Document | What it tells you |
|---|---|
| Business Case | Why the project exists, its scope and success criteria |
| Stakeholder Analysis | Who is involved; the stakeholder IDs used for owners and reviewers |
| Project Plan | The gateways and their schedule |
| Milestones | One document per gateway: Go/No-Go criteria and tasks |
| Artifact registry | Where each artifact type lives, the next version, the PO language and domain |
| Traceability matrix | How the artifacts link and when each was last reviewed |
| Framework README | The framework this project uses |
| Framework changelog | What changed in each framework release |