Home > Enterprise >  I am using the pyttsx3 API. I am creating filenames in for loop and adding mp3 extension. While file
I am using the pyttsx3 API. I am creating filenames in for loop and adding mp3 extension. While file

Time:11-20

Essentially, I am iterating through a list and want to save an mp3 file for each element in the list. The file for each element would basically be reading out the list element. The name it would be saved under is the text it reads the mp3 extension.

The textSpeech comes from the pyttsx3 module. I installed this in Pycharm by going to the terminal and typing pip install pyttsx3.

Here is my code, with just the for loop part shown.

import pyttsx3
textSpeech = pyttsx3.init()    
for x in dialogueColumns:
    words = df[x].values.tolist()
    for j in range(6, len(words)):
        name = "Step "   str(first[j])   ": "   words[5]   ".mp3"
        textSpeech.save_to_file(words[j], name)
        textSpeech.runAndWait()

I have an example here that essentially replicates what I need:

 import pyttsx3
 textSpeech = pyttsx3.init()    
 names = ["this", "is", "a", "list", "of", "file", "names"]
 for index in range(0, len(names)):
     fileName = names[index]   ".mp3"
     # In the below line, the first input is the text to be spoken
     # the second input is the file name with the mp3 extension
     textSpeech.save_to_file(names[index], fileName)
     textSpeech.runAndWait()

CodePudding user response:

Your problem is not in the code, it is in the configuration of your Windows explorer.

Open the Windows File Explorer, select View, Options, select the View tab, find the option Hide extensions for known file types and make sure it is not checked. Uncheck it otherwise and select OK.

Rerun your script and you'll likely find that the files you're writing do indeed have the mp3 extension.

The problem with your code is straightforward: not only is there a space in your filename (which should be fine in most cases), but there's a colon there as well, and that's not allowed.

name = "Step "   str(first[j])   ": "   words[5]   ".mp3"
  • Related