nateshmbhat / pyttsx3

Offline Text To Speech synthesis for python
Mozilla Public License 2.0
2.09k stars 330 forks source link

I have not been able to get save_to_file to work on Win10. Please provide a complete, but minimal example. I am a Python novice. Apparently, many others are having the same problem. Downloaded pyttsx3 2019-Oct-18. #71

Closed Daedal07 closed 4 years ago

Daedal07 commented 4 years ago

I have not been able to get save_to_file to work on Win10. Please provide a complete, but minimal example. I am a Python novice. Apparently, many others are having the same problem. Downloaded pyttsx3 2019-Oct-18. Thanks. pyttsx3_2file_fails.txt

Originally posted by @spetr0s in https://github.com/nateshmbhat/pyttsx3/issues/7#issuecomment-545178178

blueye4k commented 4 years ago

Same here. Please someone help!

GNUGradyn commented 4 years ago

save_to_file does not seem to be outputting anything at all.

Python 3.7.5 (tags/v3.7.5:5c02a39a0b, Oct 15 2019, 00:11:34) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyttsx3
>>> engine = pyttsx3.init()
>>> engine.save_to_file("This is a test!", "test")  

This does not create a file, does not output anything in the console, or appear to do anything at all

chintan70-1 commented 4 years ago

Same

Themis3000 commented 4 years ago

@GNUGradyn's code seems to do absolutely nothing for me as well

I've tried running their code in python 3.6 and 3.7.6 on windows 10 to no avail

alexander-e-andrews commented 4 years ago

In order to get save_to_file to work, I had to install the package from github instead of pip: pip install git+git://github.com/nateshmbhat/pyttsx3.git

Then my example code:

import pyttsx3
import os
fullPath = os.path.join(os.getcwd(), "bar.wav")

engine = pyttsx3.init()
engine.save_to_file("Foo bar bar foo bar", fullPath)
engine.runAndWait()
Themis3000 commented 4 years ago

Using the method that @alexander-e-andrews used didn't work for me, I am unable to initilizepyttsx3. The following code produced this error for me: https://pastebin.com/4JyTqMFZ

import pyttsx3

engine = pyttsx3.init()
alexander-e-andrews commented 4 years ago

Try looking at this thread: https://github.com/nateshmbhat/pyttsx3/issues/29 Especially https://github.com/nateshmbhat/pyttsx3/issues/29#issuecomment-445073967 If I had to guess python cannot find a voice engine on your computer.

nateshmbhat commented 4 years ago

"""Saving Voice to a file"""

On linux make sure that 'espeak' and 'ffmpeg' are installed

engine.save_to_file('Hello World', 'test.mp3') engine.runAndWait()

sachin-acharya-projects commented 3 years ago
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() # Donot forget to add this line
shakhrom96bs commented 2 years ago

I have also desame Problem with engine.save_to_file -> attributeError: 'Engine' object has no attribute 'save_to_file' Please can help somebody

madhuwantha commented 2 years ago

This worked with .wav file form

```
engine = pyttsx3.init()
string = \"Some test\"
voices = engine.getProperty("voices")[0]
engine.setProperty('voice', voices)
# engine.say(string)
engine.save_to_file(string, 'speech.wav')
engine.runAndWait()
gaurav-95 commented 2 years ago

@sachin-acharya-projects Thanks a lot, the last line seems essential for it to run!

narspt commented 1 year ago

I have the same problem, curiously if I have a say() after save_to_file() it works fine but if I omit the say() or have it before save_to_file() then no file is created at all, makes me think that maybe for some reason it does't wait (despite runAndWait() is there) and script finish before saving the file?

jonfon96 commented 7 months ago

I have the same issue when I run my script it seems to overwrite the mp3 with the last block of data. I watch as the mp3 is created and look at the size as it grows then shrinks. I try to move the speak,runAndWait() outside of the for loop but I do not see the mp3 grow as I see when I run it with gTTS but since there is a cap on access I would like to use this option. the end result is I have an mp3 with 1k in size and I have 80 page pdf I want to convert.

import pyttsx3 import PyPDF2

pdfObj = open('ch5.pdf','rb')

pdfreader = PyPDF2.PdfReader(pdfObj)

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

for page_num in range(len(pdfreader.pages)): text = pdfreader.pages[page_num].extract_text() ## extracting text from the PDF cleaned_text = text.strip().replace('\n',' ') ## Removes unnecessary spaces and break lines print(cleaned_text)

speaker.say(cleaned_text) ## Let The Speaker Speak The Text

#os.rename('temp', "ch3.mp3")
speaker.save_to_file(cleaned_text,'ch5.mp3')  ## Saving Text In a audio file 'story.mp3'

speaker.runAndWait() # we will move this outside the for loop to see if it works.

speaker.stop()

jonfon96 commented 7 months ago

I tried to swap the "say function" line with the "save_to_file" line but I still get small blocks of data saved to the mp3 but it still does not append to make the mp3 grow but keeps it small and overwrites this data.

jonfon96 commented 7 months ago

Hello All I was able to adapt a gtts script to pytts script and use it function call but what I found it stops at 21 pages even after I remove some of the new lines to make is stremline. I still have an issue with it not converting the whole file to mp3. if someone know how I can get pass this that will help.

import PyPDF2 import pyttsx3 from gtts import gTTS import os

speaker = pyttsx3.init()

def extract_text_from_pdf(pdf_path): text = "" with open(pdf_path, "rb") as f: reader = PyPDF2.PdfReader(f) num_pages = len(reader.pages)

num_pages = reader.numPages # was replaced

    for page_num in range(num_pages):
        page = reader.pages[page_num]
        text += page.extract_text() 
        clean_text = text.strip().replace('\n',' ') # added this line to clean the spaces and new lines. 
return clean_text

def convert_text_to_speech(text, output_path): speaker.save_to_file(text, output_path) speaker.runAndWait() speaker.stop() return output_path

def pdf_to_speech(pdf_path, output_path): text = extract_text_from_pdf(pdf_path)
convert_text_to_speech(text, output_path)

if name == "main": pdf_file = "ch5.pdf" # Path to your PDF file output_file = "ch5.mp3" # Output path for the generated speech file pdf_to_speech(pdf_file, output_file) print(f"PDF converted to speech. Output saved to: {output_file}")