Sep-22-2023, 07:43 PM
(Sep-22-2023, 05:26 PM)deanhystad Wrote: Looking at the source (I don't want to install the package), Engine.save_to_file() returns None. That makes this code nonsinsical.
audio_file = speaker.save_to_file(clean_text, audio_name + '.mp3')What made you think it returned a str?
What kind of magic was this meant to do?
move_file = os.path.join(program_path, save_path, audio_file)Was this supposed to move a file audiofile from program_path to save_path? Did you read the documentation for os.path.join? os.path.join is normally used to append a filename to a file path.
What is the purpose of this?
program_path = "C:\\ . . \\Python learning center\\automating_1" save_path = "C:\\ . . \\Python learning center\\automating_1\\Audio"You should rarely hard code file paths, especially not a path that appears to be in a user folder. You cannot move your file. Nobody else can run your program. Use relative paths. Normally I use no path, or "." if a path is required. If you want to use the folder that contains your program file, use "__file__" in your program. Python replaces "__file__" with the full filename
current_directory = pathlib.Path(".") program_folder = "pathlib.Path(__file__).parentYour program saved the mp3 file in the current directory. This might be the program_path, but it might not. You don't need to know the program path anyway. Save the file directly to the save folder.
You should hardly ever import "os". Most of the things "os" was used for now have os independent packages that work better. Instead of os.path, us pathlib. And don't use backslashes in paths. There is just too much messiness with \ being a separator, the start of an escape sequence, having special meaning in regular expressions... Use forward slash instead. Windows doesn't care what slash you use.
import pyttsx3 from PyPDF2 import PdfReader from pathlib import Path # Select a pdf file. Extract first page. print(*Path().glob("*.pdf"), sep="\n") bookname = input("choose a file. . ") reader = PdfReader(bookname, "rb") number_of_pages = len(reader.pages) page = reader.pages[0] text = page.extract_text() clean_text = text.strip().replace("\n", "") # Convert pdf text to speech. Save as mp3. Read text. speaker = pyttsx3.init() audio_name = input("name the audio file: ") save_path = Path(__file__).parent / "Audio" audio_file = speaker.save_to_file(clean_text, save_path / f"{audio_name}.mp3") speaker.runAndWait()
thank you for your help !