Coding Education STEM

Text-to-Speech (TTS) and Pandas (POTUS data)

In this post, I introduce you to my Python 3.x program that reads an external data file containing all president names of the USA to-date (2022), and that allows users to enter a query to and hear the names of presidents as the result. The query can be a number such as a first N presidents, or last N presidents, or Nst/th president, or all presidents to date. Spoken audio is in American English and can also be saved as a mp3 file to the user’s folder in “Data” subdirectory as “POTUS-TTStest3.mp3”.

After it loads the data file, it searches in memory for finding the value by user-entered index to get the results. Libraries used: pyttsx3, pandas

This is both and an educational application from programming as well as from a historical perspective. This core codebase can be easily extended to a graphical user-interface, web application and mobile application. The complete source code is available on my Gitbub Repo here.

Example snapshots of running it in a shell is below.

For example, to get a specific Nth president, say 35th president of USA, at the prompt enter ‘#35’ (without quotes) and the output will be:

>>> %Run TextToSpeech3-interactive.py
Total United States presidents on record:  46

Which presidents' names do you want read aloud (pick a number from menu)?
[For first 2 presidents, enter 2. For the last 2 presidents, enter -2. To get 10th president, enter: #10. And so on. To hear all presidents' names enter: 99] [Quit/Cancel:0 or ENTER]: #35
Number 35 president: John F. Kennedy

The name of that president is spoken aloud and saved in the user’s Data subdirectory as POTUS-TTStest3.mp3 file, which sounds like this:

Audio output:
 

To get the first 5 presidents of USA, we would enter at the prompt: 5

and the output would be in text and spoken in American English and saved as a mp3 file:

Showing FIRST 5 presidents: 
  George Washington
        John Adams
  Thomas Jefferson
     James Madison
      James Monroe

To get the last 3 (most recent) presidents, we would enter: -3

and the output would be in text and spoken and saved as a mp3 file:

Showing LAST 3 presidents: 
  Barack Obama
 Donald Trump
    Joe Biden

To get the full list of USA presidents spoken out to you as well as listed on screen and saved as a mp3 file, just enter 99 at the prompt.

I hope you have fun exploring this code on Github. I will provide more explanation of the code possibly in a future book or blog.



Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top