how to record pyttsx3 file using python

Solutions on MaxInterview for how to record pyttsx3 file using python by the best coders in the world

showing results for - "how to record pyttsx3 file using python"
Giacomo
25 Jan 2017
1def txt_zu_wav(eingabe, ausgabe, text_aus_datei = True, geschwindigkeit = 2, Stimmenname = "Zira"):
2    from comtypes.client import CreateObject
3    engine = CreateObject("SAPI.SpVoice")
4
5    engine.rate = geschwindigkeit # von -10 bis 10
6
7    for stimme in engine.GetVoices():
8        if stimme.GetDescription().find(Stimmenname) >= 0:
9            engine.Voice = stimme
10            break
11    else:
12        print("Fehler Stimme nicht gefunden -> Standard wird benutzt")
13
14    if text_aus_datei:
15        datei = open(eingabe, 'r')
16        text = datei.read()
17        datei.close()
18    else:
19        text = eingabe
20
21    stream = CreateObject("SAPI.SpFileStream")
22    from comtypes.gen import SpeechLib
23
24    stream.Open(ausgabe, SpeechLib.SSFMCreateForWrite)
25    engine.AudioOutputStream = stream
26    engine.speak(text)
27
28    stream.Close()
29
30txt_zu_wav("test.txt", "test_1.wav")
31txt_zu_wav("It also works with a string instead of a file path", "test_2.wav", False)
32
Stefano
13 Jan 2018
1import pyttsx3
2from gtts import gTTS
3
4engine = pyttsx3.init(driverName='sapi5')
5infile = "tanjil.txt"
6f = open(infile, 'r')
7theText = f.read()
8f.close()
9
10#Saving part starts from here 
11tts = gTTS(text=theText, lang='en')
12tts.save("saved_file.mp3")
13print("File saved!")
14