Skip to content

Instantly share code, notes, and snippets.

@gkhays
Last active March 14, 2019 01:46
Show Gist options
  • Save gkhays/502c03b100bc8eb7cefd92bdc579834e to your computer and use it in GitHub Desktop.
Save gkhays/502c03b100bc8eb7cefd92bdc579834e to your computer and use it in GitHub Desktop.
Re-implementation of the famed Audio Ghost in Python. For Audio Ghost, see https://github.com/gkhays/AudioGhost.

Text to Speech in Python

A quick project to convert text files to speech.

Getting Started

A best practice in Python is to not "pollute" your entire workspace with Python modules. Instead, segregate them per project. Accordingly, set up and activate a virtual environment.

$ virtualenv .venv
$.venv/Scripts/activate.bat

Install the Google text to speech API.

pip install gTTS

Run the app.

python demo.py

When you are finished, exit the virtual environment.

deactivate

Next Steps

I started out with the Google text to speech API but the win32com.client sounds more like AudioGhost.

pip install pypiwin32

Code

import win32com.client

speaker = win32com.client.Dispatch("SAPI.SpVoice")

while 1:
    print("Enter the word you want to speak")
    s = input()
    speaker.Speak(s)

# Stop with CTRL + Z

References

import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
while True:
print("Enter word or phrase >>")
s = input()
if s.strip() == 'quit' or s.strip() == 'exit':
break
speaker.Speak(s)
# Enter quit or exit on a single line to stop.
Hello earthling, I am glad you came to me. Let us talk. OK bye.
from gtts import gTTS
import os
with open('data.txt', 'r') as datafile:
text = datafile.read()
language = 'en'
engine = gTTS(text=text, lang=language, slow=False)
engine.save('data.mp3')
os.system("start data.mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment