2
Step 4 Convert and Command Line
Tirsvad edited this page 2026-11-20 17:50:12 +01:00

English · Dansk · Bahasa Malaysia

Step 4: Convert and command line

Step 4: convert a PDF to an audiobook

What we did

  • convert_pdf_to_audiobook(pdf_path, output_path, engine, extract_text, progress) is the assignment's function: extract, split, speak each piece, join the clips and write one WAV file.
  • join_wav_clips joins the clips with the standard library wave module and refuses clips with different audio settings.
  • The command line is the composition root: it is the only place that builds the real engine and the real PDF reader.
python -m pdf_to_audiobook tests/data/sample.pdf -o build/sample.wav
pdf-to-audiobook --help

Exit codes: 0 success, 1 an error with a message on stderr, 2 wrong use of the command.

What we gained

  • Dependencies point inward: the use case knows the port and a function, not Coqui TTS or pypdf.
  • Progress and errors go to stderr and the result to stdout, so the program works in scripts.
  • A real run on the two page sample PDF gave about 17 seconds of speech.
  • The console stays quiet: the speech library prints phoneme text, warnings and download bars, so the program hides them and shows only its own lines. Add --verbose to the command or to the window to see the library output again when you diagnose a problem. Real errors are always shown.