Skip to content

Instantly share code, notes, and snippets.

@monkrus
Last active June 25, 2023 06:17
Show Gist options
  • Select an option

  • Save monkrus/91726c39bc396b3e8c073879a4cd7849 to your computer and use it in GitHub Desktop.

Select an option

Save monkrus/91726c39bc396b3e8c073879a4cd7849 to your computer and use it in GitHub Desktop.
Text to voice
from gtts import gTTS
import os
def text_to_speech(file_path):
try:
with open(file_path, 'r') as file:
text = file.read().replace('\n', ' ')
speech = gTTS(text=text, lang='en', slow=False)
speech.save("output.mp3")
os.system("start output.mp3")
except FileNotFoundError:
print(f"Sorry, the file {file_path} does not exist.")
except Exception as e:
print(str(e))
if __name__ == "__main__":
text_file = input("Please enter the path to the text file: ")
text_to_speech(text_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment