How I built An Audio Reader CLI App In Python

How I built An Audio Reader CLI App In Python

I used Pyttsx3, Beautiful Soup & Requests To Achieve This

Hey there 👋, welcome back if you are a usual suspect! And If you are new here, sit tight! I am friendly and hope you enjoy my article today.

So I wanted to listen to some articles instead of reading them while I am doing some work. Yeah, multitasking...

image.png And I thought about building my own audio reader because I like building my own programs nowadays given the python skills I have on top of Python's huge library.

Let's Get Started 🚀

An Audio Reader App is basically text to speech translator and there are tons of apps out there you could get, some are freemium & others premium.

Tools 🛠

📌 pyttsx3 - Text-to-speech x-platform
📌 requests -Make HTTP requests to any API in the world
📌 bs4 -Pulling data out of HTML and XML files

Let's build it together:

I created an audible.py file on my desktop
Import dependencies

import pyttsx3
import requests
from bs4 import BeautifulSoup

Main Engine Initialising 🛫:

engine = pyttsx3.init('sapi5')

The init command helps to get a reference to a pyttsx3.Engine instance.
It takes a parameter which is a driverNamesapi5

Some of the pyttsx3.drivers module to load and use currently are:
sapi5 - SAPI5 on Windows
nsss - NSSpeechSynthesizer on Mac OS X
espeak - eSpeak on every other platform
Read More

Voice Getter & Setter 🔊

# Get voices list property from the engine
voices = engine.getProperty('voices')

# setting voice to the first one in the list(female)
engine.setProperty('voice', voices[0].id)

Create a function to handle audio output 🎵

def speak(audio):
    engine.say(audio)
    engine.runAndWait()

The say() queues a command to speak an utterance.
The runAndWait() blocks while processing all currently queued commands.

Get Article URL from user input 🖍

url = str(input("Paste article URL : \n"))
print("\n -------------------------------")

# Get text using requests
data = requests.get(url).text

# print(data) -> This outputs html markup

Test URL: Freecode Camp Link

Getting the soup {text} from the html page(data) 🍳

soup = BeautifulSoup(data,'html.parser')
article = soup.get_text()

print(article)

And the golden pie 🍕. Let's get our audio

print(speak(article))

# If you want to save the speech as an audio file
engine.save_to_file(article, 'article.mp3')

engine.runAndWait()

The mp3 is saved at the end of the program & is located in the cwd .

Github Repo: Audio Reader App

Watch Video Implementation: Audio Reader App

Conclusion

That's It you have built your own audio reader in just a few lines of python using only three libraries. My challenge to you will be: find a way how to implement audio sync with text being logged out in the console to match line by line.

Tip: Try to search for time or await or async or anything related to this:

Now I am thinking of the GUI app. I wanna built it too. Stay In touch >>>

If you enjoyed this simple article, kindly share the love! Subscribe & comment for any issues. See You!

Ronnie Atuhaire 😎