Search code examples
python-3.xstringtext-to-speechpyttsx3

python3 pyttsx3 formatting a string with apostrophes


I have a list of strings that I want to pass to a tts module but don't know how to format the string so that expressions like "it's" or "you'll" will be interpreted correctly.

Currently the tts module reads it as "it backslash s" and "you backslash l l".

Code example:

import os
import re
import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)

user_profile = os.environ['USERPROFILE']
folder_path = user_profile + r"\documents\outputTest"
if not os.path.exists(folder_path):
    os.makedirs(folder_path)

rx = re.compile(r'_{2,}')
text = ['"try this _____text. you\'ll need it."']
newText = [rx.sub(r"_", i).replace("\\","") for i in text]#the line in question

engine.save_to_file(newText, folder_path + r'\test.mp3')
engine.runAndWait()

Expected example

import os
import pyttsx3

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)

user_profile = os.environ['USERPROFILE']
folder_path = user_profile + r"\documents\outputTest"
if not os.path.exists(folder_path):
    os.makedirs(folder_path)

engine.save_to_file(r"Try this text. you'll need it.", folder_path + r'\workingTest.mp3')
engine.runAndWait()

Solution

  • I found a working solution.

    newText = [rx.sub(r"_", i).replace("\\","").replace('"',"") for i in text]
    

    by removing double quotes the outer single quotes get converted to double quotes and the apostrophe becomes one without backslash. In effect the tts module can interpret it correctly.

    This is a working solution as the double quotes are not voiced out by the tts module and are therefore not relevant to keep.