8

Here is my Python code:

import pyttsx3

engine = pyttsx3.init(driverName='sapi5')
f = open("tanjil.txt", 'r')
theText = f.read()
f.close()
engine.say(theText)
engine.runAndWait()

I couldn't save the file to an audio file.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
taaanjil
  • 81
  • 1
  • 1
  • 2

4 Answers4

6

As of July 14 2019, I'm able to save to file with the pyttsx3 library (without using another library or internet connection).

It doesn't appear to be documented, but looking at the source code in github for the Engine class in "engine.py" (https://github.com/nateshmbhat/pyttsx3/blob/master/pyttsx3/engine.py), I was able to find a "save_to_file" function:

def save_to_file(self, text, filename, name=None):
    '''
    Adds an utterance to speak to the event queue.
    @param text: Text to sepak
    @type text: unicode
    @param filename: the name of file to save.
    @param name: Name to associate with this utterance. Included in
        notifications about this utterance.
    @type name: str
    '''
    self.proxy.save_to_file(text, filename, name)

I am able to use this like:

engine.save_to_file('the text I want to save as audio', path_to_save)

Not sure the format - it's some raw audio format (I guess it's maybe something like aiff) - but I can play it in an audio player.

If you install pydub: https://pypi.org/project/pydub/

then you can easily convert this to mp3, e.g.:

from pydub import AudioSegment
AudioSegment.from_file(path_to_save).export('converted.mp3', format="mp3")
Brian
  • 282
  • 4
  • 10
6
import pyttsx3
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")[0] 
engine.setProperty('voice', voices)
text = 'Your Text'
engine.save_to_file(text, 'name.mp3')
engine.runAndWait() # don't forget to use this line
5

I've tried @Brian's solution but it didn't work for me.

I searched around a bit and I couldn't figure out how to save the speech to mp3 in pyttx3 but I found another solution without pyttx3.

It can take a .txt file and directly output a .wav file,

def txt_zu_wav(eingabe, ausgabe, text_aus_datei = True, geschwindigkeit = 2, Stimmenname = "Zira"):
    from comtypes.client import CreateObject
    engine = CreateObject("SAPI.SpVoice")

    engine.rate = geschwindigkeit # von -10 bis 10

    for stimme in engine.GetVoices():
        if stimme.GetDescription().find(Stimmenname) >= 0:
            engine.Voice = stimme
            break
    else:
        print("Fehler Stimme nicht gefunden -> Standard wird benutzt")

    if text_aus_datei:
        datei = open(eingabe, 'r')
        text = datei.read()
        datei.close()
    else:
        text = eingabe

    stream = CreateObject("SAPI.SpFileStream")
    from comtypes.gen import SpeechLib

    stream.Open(ausgabe, SpeechLib.SSFMCreateForWrite)
    engine.AudioOutputStream = stream
    engine.speak(text)

    stream.Close()

txt_zu_wav("test.txt", "test_1.wav")
txt_zu_wav("It also works with a string instead of a file path", "test_2.wav", False)

This was tested with Python 3.7.4 on Windows 10.

Nexarius
  • 346
  • 4
  • 11
  • So you just found it on the internet? Could you edit the link where you've found it into your post? – csabinho Nov 16 '19 at 22:48
  • Oh I didn't mean that I literally found this function on a website and just copy pasted it. I made this. – Nexarius Nov 16 '19 at 23:10
  • 1
    Kindly mention, this is only working for Windows but not Mac, as the library COMTypes is designed for Windows, not Linux https://stackoverflow.com/questions/37161560/importerror-cannot-import-name-comerror-in-python – vera Jan 06 '20 at 03:40
  • 1
    please use english var names so everyone can understand it. – Fritz Nov 23 '22 at 19:52
1

Try the following code snippet to convert text to audio and save it as an mp3 file.

import pyttsx3
from pydub import AudioSegment

engine = pyttsx3.init('sapi5')
engine.save_to_file('This is a test phrase.', 'test.mp3') # raw audio file
engine.runAndWait()

AudioSegment.from_file('test.mp3').export('test.mp3', format="mp3") # audio file in mp3 format 

NB: pyttsx3 save_to_file() method creates a raw audio file and it won't be useful for other applications to use even if we are able to play it in the media player. pydub is a useful package to convert raw audio into a specific format.

Codemaker2015
  • 12,190
  • 6
  • 97
  • 81