nateshmbhat / pyttsx3

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

save_to_file not work after i use in loop #283

Open nhman482 opened 1 year ago

nhman482 commented 1 year ago

this is the code and it save only one file instead of save two

import uuid import pyttsx3

engine = pyttsx3.init()

def save_speech_file(text: str): """ :param text: The text to be saved as speech. :return: None """ try: filename = f'{uuid.uuid4()}.mp3' engine.save_to_file(text, filename) print(f'Saved speech as {filename}') engine.runAndWait() finally: engine.stop()

for i in range(2): save_speech_file('test')

Jiangshan00001 commented 1 year ago

which os are you using? if macos, you have to try:

pip install pyobjc==9.0.1

Cerz0 commented 11 months ago

i'm having the same issue but on windows, just not save_to_file, "say" aso doesn't work inside the loop

jonfon96 commented 4 months ago

I have a similar issue when I place it in a for loop the output is not appended but over written so the end file is only the last block of data that was converted. I have look at other code online and youtube but they all have one page they are saving . I have 80 pages of pdf that I want to save as a mp3 or wav. I also change it to include pyttsx4 but that did the same. Os used. Win 10 with 32 gig ram and all the latest updates. Centos 9 with 24 gig ram and all the latest updates.

code enclosed below. import PyPDF2 # or from PyPDF4 import PdfFileReader import pyttsx3

speak = pyttsx3.init() # we are going to init the pyttsx3 and call it 'speak' which we are going to use below to convert to speak and save as mp3.

def read_pdf(path): # this is the creation of a function with open(path, 'rb') as f: # we will use a context manager to open the file and get the path from the user in mode 'rb'=(read bianary) and save it as 'f' the file pdf_read = PyPDF2.PdfReader(f) # we created a varible called pdf_read and we will use the PdfFileReader function to read the content of the file 'f' we defile above for x in range(len(pdf_read.pages)): # we will create a for loop for the total number of pages but we will use the "pdf_read.getNumPages()" to get that number and pass it into the for loop page = pdf_read.pages[x] # we are getting each page in the for loop since we are using 'x' as the page_content = page.extract_text() # this will extract the text from each page we are reading in the page varaible.

print(page_content) #we can print the page_content with the print function we will also comment out this line once we know it works.

        speak.save_to_file(page_content, 'ch5.mp3') # this will take two variables the page_content we use to extract text from file and the other is the name of the mp3 or audio file. 
        speak.runAndWait()  # this will run and wait 
        speak.stop()        # this stops it all. 

read_pdf('ch5.pdf') # we are calling the read_pdf function and it will output the content on the screen.

chelizichen commented 1 month ago

which os are you using? if macos, you have to try:

pip install pyobjc==9.0.1

It works success on my mac machine, thanks