0
import csv
import pyttsx3

engine = pyttsx3.init()


with open('file.csv', 'r', encoding='utf8') as csv_file:
    csv_reader = csv.reader(csv_file)

    next(csv_reader)

    for line in csv_reader:
        title = line[1]
        text = line[5]
        print(title,text)
        while True:
            speech = (title,text)
            engine.say(speech)
            engine.runAndWait()

CODE ABOVE:

The goal of my code is to read text from a csv file. It does work however there is an issue whereby the text-to-speech playback will include saying things like "backslash n" and other non-text items. Is there a way to format my csv file to make it better to read or do I use another text-to-speech library?

Any help appreciated, this is one of my first python projects.

1 Answers1

0

I am not sure how pyttsx3 is implemented because I have not used it before but from what I gather from the documentation it was meant to take input from the user and not really csv files.

What you can try is to just remove the last character from each line as you feed it into the function. So for example your line looks like this,

text = "hello world\n"

or

speech = "hello world\n"

Just change either one of them like this,

text = text[:-1]

or

speech = speech[:-1]

That should get rid of the new line character and it will be just be hello world.

anarchy
  • 3,709
  • 2
  • 16
  • 48