I like to read on Wikipedia a lot and also i happen to be a very lazy person but sometimes i wanna find out about a certain topic or person, I know how fun it is to automate tasks so why don’t we make a Wikipedia bot that will read the article for us….Seriously though it will read with an actual human voice
The idea of automation is such an interesting topic and has endless ideas and i decided to explore it myself and found a problem i personally have and can solve easily with automation so let’s not waste any further time and get to coding
Libraries we’re gonna use in this tutorial
- pyttsx3 : To help us do TTS -text to speech-
pip install pyttsx3
- WikipediaAPI : a wrapper for Wikipedia API written in Python
pip install Wikipedia-API
- Click : to create an easy to use CLI of our script
pip install click
The bot will simply fetch the summery of an article then reads it out for us and in later article we will add a gui to the script for those non-programming people who just wanna enjoy the final product
Now create a new Python file and start coding
from wikipediaapi import Wikipediaimport pyttsx3wiki = Wikipedia("en")eng = pyttsx3.init()wiki_page = wiki.page("elon musk").summaryeng.say(wiki_page)eng.runAndWait()
Now each program using the pyttsx3
starts with an engine object and ends with runAndWait()
method to start executing
We also need a Wikipedia object to interact with the module and retrieve the data we want
page
represents a Wikipedia page which has many attributes like title
and summery
which in this case we used so we pass it to our speech engine object
So now let’s make this program more dynamic by firstly using the input()
function to get the user’s input, so adding this simple line
wiki_page = input("What do you wanna search for today? ")
then we can just simply eng.say(wiki_page.summery)
Now another way to make this more dynamic is using click
to make a CLI tool out of this
import clickfrom wikipediaapi import Wikipediaimport pyttsx3wiki = Wikipedia('en')eng = pyttsx3.init()@click.command()@click.option("--title",prompt="Enter an article's title to search")def get_article(title): wiki_page = wiki.page(title=title).summary eng.say(wiki_page) eng.runAndWait()if __name__ == "__main__": get_article()
And there you have it, a very simple yet fun bot to enjoy listening to Wikipedia articles whenever you want
Further Improvements
- Since we saw how easy it is to use pyttsx3, we can now scrape data online like songs lyrics and can now make it sing !!!! (kinda)
- Generate audio books now from your favorite PDFs with the
save_to_file()
function - Make a gui for a Wikipedia browser where you can view an article’s sections and a quick summery
Just a glimpse of what you can do with those amazing tools and the list can really go on if you’re creative enough
Thanks for the time and interest in my article :)