Now I tried to save the audio output into wav files (mp3 file cannot play, no sound), but it play the audio but does not save audio files at all. Also this program eventually halted when all files under TTBOD folder are exhausted. I can see the mac wheel spinning when I go to the latest popup window. I suspect the halting might be the reason that mp3 files cannot be save since I can save the .wav file when I only use one document and don't go through all documents. But I am not sure. Can anyone help me to debug the codes? Thank you.
`
import tkinter as tk
import pyttsx3
import docx2txt
import re
import os
import time
# Create a window
window = tk.Tk()
window.title("Please provide the gender and Name of the subject")
window.geometry("500x300")
window.configure(bg='navyblue')
docx_files = []
os.chdir("/Users/alex/Desktop/TTBOD/M")
for file in os.listdir():
if file.endswith(".docx"):
docx_files.append(file)
docx_files.sort()
concatenated_files = []
#####################################################
def narration(doc, voc, fn):
engine = pyttsx3.init()
# Set the volume and rate of the speech
engine.setProperty('volume', 1.0)
engine.setProperty('rate', 180)
# voice_id = "/System/Library/LaunchDaemons/com.apple.locate.plist/com.apple.speech.synthesis.voice.Alex"
voices = engine.getProperty('voices')
if voc == 'male':
engine.setProperty('voice', voices[0].id)
engine.say(doc)
engine.runAndWait()
engine.save_to_file(doc, "{}.wav".format(fn))
engine.stop()
time.sleep(2) # pause for 2 seconds before narrating next document
#####################################################
def submit():
# documents = []
gender = gender_var.get()
name = name_entry.get()
voice = gender_var2.get()
# result_label.config(text=f"Narrator's gender is {voice} and your name is {name}.")
for file in docx_files:
fname=file
if gender =='male':
concatenated_files.append("/Users/alex/Desktop/TTBOD/M/" + file)
else :
concatenated_files.append("/Users/alex/Desktop/TTBOD/F/" + file)
for file in concatenated_files:
document = docx2txt.process(file)
# Replace "So-and-so" with the subject's name
if re.search("So-and-so", document):
newdoc = re.sub("So-and-so", name, document)
if re.search("so-and-so", newdoc):
newdoc = re.sub("so-and-so", name, newdoc)
document = newdoc
# Hide the main window
window.withdraw()
# Create a new window to display the document
popup = tk.Tk()
popup.title("Document")
popup.geometry("1700x1000")
popup.configure(bg='black')
# Create a scrollbar
scrollbar = tk.Scrollbar(popup)
# scrollbar.pack()
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# Create a text widget to display the document
result_text = tk.Text(popup, font="ariel 20", fg='white', bg='black', yscrollcommand=scrollbar.set, wrap=tk.WORD)
result_text.pack(expand=True, fill=tk.BOTH)
# Set the scrollbar to the text widget
scrollbar.config(command=result_text.yview)
# Insert the document text into the text widget
result_text.insert(tk.END, document)
# Update the window to display the result_text widget
popup.update()
# Play the speech
narration(document, voice, fname)
# Create a variable to store the gender selection
gender_var = tk.StringVar()
gender_var2 = tk.StringVar()
# Create a label for the gender entry
gender_label = tk.Label(window, font="ariel 20 bold", text="Choose the gender of the subject:", fg='white', bg='navyblue')
gender_label.pack()
# Create a dropdown menu for the gender
gender_menu = tk.OptionMenu(window, gender_var, "male", "female")
gender_menu.config(width=5)
gender_menu.pack()
# Create a label for the name entry
name_label = tk.Label(window, font="ariel 20 bold", text="Please provide the name of the subject:", fg='white', bg='navyblue')
name_label.pack()
# Create a entry for the name
name_entry = tk.Entry(window, fg='white', bg='navyblue')
name_entry.pack()
# Create a label for the gender entry
gender_label2 = tk.Label(window, font="ariel 20 bold", text="Narrator's gender:", fg='white', bg='navyblue')
gender_label2.pack()
# Create a dropdown menu for the gender
gender_menu2 = tk.OptionMenu(window, gender_var2, "male", "female")
gender_menu2.config(width=5)
gender_menu2.pack()
# Create a button to submit the entries
submit_button = tk.Button(window, font="ariel 20 bold", text="Submit", command=submit, fg='black', bg='navyblue')
submit_button.pack()
# Create a label to show the result
result_label = tk.Label(window, font="ariel 20 bold", text="", fg='white', bg='navyblue')
result_label.pack()
# Create a function to be called when the button is clicked
# Run the window
window.mainloop()
`
I try to save each document's narration into a MP3 file. But it does not work.