How to use Python gTTS

Asked 1 years ago, Updated 1 years ago, 100 views

I'm trying to use gts module in Python

import os from gtts import gTTS

text_to_read = "Hi"
language = 'ko'
slow_audio_speed = False
filename = "my_file.mp3"

def reading_from_user():
    user_input = input ("What should I say?")\n")
    audio_created =gTTS(text=user_input, lang=language, slow=slow_audio_speed)
    audio_created.save(filename)

    os.system(f'start {filename}')


if __name__ == '__main__':
    reading_from_user()

Questions
In gTTS, do I have to save it as an mp3 file and read it?

Can't I just play the writing on input right away?

gtts tts python

2022-09-21 15:43

2 Answers

In fact, if you're a Windows user, there's a simpler way.

Use sapi in Windows (https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms723627(v%3Dvs.85)).

Since it is a COM object, you can use the pywin32 (pip install pywin32) module.

import win32com.client
tts = win32com.client.Dispatch("SAPI.SpVoice")
tts.Spoke ("Hashcode requires answer search functionality.")


2022-09-21 15:43

If you look at the official document, there is a separate entry "Play Sound Right Now".

We have quite a few libraries to make this possible. Write "Hello" on the file resource object and try adding more.

HELP: Issue #26 for discussions and examples on how to play sound.

from gtts import gTTS
from io import BytesIO

mp3_fp = BytesIO()
tts = gTTS('hello', 'en')
tts.write_to_fp(mp3_fp)

# You can now open 'mp3_fp' by treating it as mp3 in the desired audio library.


2022-09-21 15:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.