I’m studying financial advice and there’s lots of reading. I like to listen to audio as I walk but using the defaul screen reader technology on a mobile app doesn’t work this way. Once you put the phone to sleep it stops the reader.
So I started google how to convert PDF’s to audio. This blog post is about the extra stuff I had to install to get a script to work.
Table of Contents
Converting text books to PDF
I have purchased legit copies of my text books, why are ebooks the same price as physical copies? And why doesn’t a physical copy come with a free PDF version?
I’m storing all these files in a google drive folder so I can access them from any device.
Converting PDF to Audio; Python Script
from tkinter import Tk from tkinter.filedialog import askopenfilename import pdftotext from gtts import gTTS Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing filelocation = askopenfilename() # open the dialog GUI with open(filelocation, "rb") as f: # open the file in reading (rb) mode and call it f pdf = pdftotext.PDF(f) # store a text version of the pdf file f in pdf variable string_of_text = '' for text in pdf: string_of_text += text final_file = gTTS(text=string_of_text, lang='en') # store file in variable final_file.save("Generated Speech.mp3") # save file to computer
I’m using my Ubuntu v18.04.5 LTS machine.
First I needed to install python3, then a bunch of extra stuff. Here is an extract of my terminal history, hopefully in an order that makes sense.
mkdir python-audio cd python-audio/ vim PDF-to-mp3.py python3 PDF-to-mp3.py //Installs sudo apt-get update -y sudo apt install python3 sudo apt install vim //I didn't have VIM sudo apt-get install python3-tk sudo apt-get install pdftotext //this didn't work sudo apt install build-essential libpoppler-cpp-dev pkg-config python3-dev pip install pdftotext //I didn't have pip installed curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py // I didn't have curl installed sudo curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo apt install curl python3 get-pip.py sudo apt-get install -y python3-testresources sudo apt-get install poppler-utils //Trying to install pdftotext I didn't have pip on my PATH, I tried fixing it but ended up using the whole path file instead vim ~/.profile vim ~/.bash_profile rm .bash_profile sudo /home/sammy/.local/bin/pip install pdftotext sudo /home/sammy/.local/bin/pip install gtts //checking version python3 -m tkinter
Links that helped figure this out
- Tkinter Tutorial
- PDFToText Docs
- How to install CURL
- How to install test resources
- PDFToText Tutorial
- Unable to locate package name
- Install pip
- Fixing environment variables
There were so many other not as useful links but these were the gold nuggets. Development can be a bit like this, you have a gazillion tabs open to fix 1 or 2 things.
I’ve now hit the Google Text to Speech API limit
I converted the first two chapters and now I get an:
gtts.tts.gTTSError: 429 (Too Many Requests) from TTS API
So now I need to figure out how to authenticate the API requests so I can continue. Good thing Google has this awesome tutorial on using python with text to voice.
Speeding up the file
By default the extract is quiet slow. There appears to be ways to speed up the voice but I need to spend a bit more time figuring it out. I’ve found an android app called music speed changer which speeds up the file. I’ve experimented with SpeedUp and audiPo app’s on iOS but I think I need to add the files to itunes before they will pick up the files.
I’ve used an online tool to speed up the file but it doubled the file size.
I hope this helps you if you are also considering this. I found it very easy to set up, it only took me an hour of googling how to fix things to get something working which is a lot quicker than starting a test automation framework from scratch.