My python .CSV to MP3 text-to-speech script is not passing text to the API correctly - how can I fix this? - python

So I've been working on a CSV to MP3 generator to create audio resources for learning French verbs. The script takes data from a csv spreadsheet and uses gTTS to generate french audio for the verbs, and english audio for the translations. This is then concatenated into an mp3 file which includes the main verb tenses.
It had been working but now it's giving me errors "No text to send to TTS API". I printed the text strings in the terminal and the script is able to see the text from the CSV file.
I'd appreciate any suggestions. The script is below. Here is a pastebin of my csv file which contains the verb data. It must be encoded as ANSI and not UTF-8 to for the TTS to read letters with accents: https://pastebin.com/nDuKHQCM
from gtts import gTTS
from pydub import AudioSegment
import csv
import os
def generate_audio(text, language):
if not text:
return AudioSegment.silent(duration=1000)
tts = gTTS(text=text, lang=language)
tts.save("temp.mp3")
return AudioSegment.from_mp3("temp.mp3")
verb_tenses = []
with open("C:\\Users\\user\\Desktop\\faire.csv") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
verb_tenses.append(row)
file_name = os.path.splitext(os.path.basename(csvfile.name))[0]
file_name_audio = generate_audio(file_name, "fr")
final_audio = file_name_audio + AudioSegment.silent(duration=2000)
for tense in verb_tenses:
if len(tense) == 1:
tense_audio = generate_audio(tense[0], "fr")
final_audio = final_audio + AudioSegment.silent(duration=1000) + tense_audio + AudioSegment.silent(duration=1000)
else:
for i in range(0, len(tense), 2):
verb = tense[i]
translation = tense[i + 1]
verb_audio = generate_audio(verb, "fr")
translation_audio = generate_audio(translation, "en")
final_audio = final_audio + verb_audio + translation_audio
final_audio.export("verb_tenses.mp3", format="mp3")
I printed the text strings in the terminal, which it did without any issues. Does the free version of gTTS have a limit on characters. There is mention of a workaround here but I don't understand how best to apply that for my use case: https://stackoverflow.com/a/71868861/19089358
The goal of this script is to generate a full reading of the verbs in the CSV file and their respective translations. Each language is read in the respective google TTS voice - Fr and Eng.
As an added bonus, it would be nice to modify the script to process separate mp3s for a folder of csv files for each verb.

Related

Gtts library error. I don't know why this error are happening or how to fix them

I am tried to convert pdf to an audio file but when ever I run my code I get a bunch error from the gtts liberary. If there is a better liberary to use that does not sound like a robot please let me know the errors are https://pastebin.com/Uwnq1MgS and my code is
#Importing Libraries
#Importing Google Text to Speech library
from gtts import gTTS
#Importing PDF reader PyPDF2
import PyPDF2
#Open file Path
pdf_File = open('simple.pdf', 'rb')
#Create PDF Reader Object
pdf_Reader = PyPDF2.PdfFileReader(pdf_File)
count = pdf_Reader.numPages # counts number of pages in pdf
textList = []
#Extracting text data from each page of the pdf file
for i in range(count):
try:
page = pdf_Reader.getPage(i)
textList.append(page.extractText())
except:
pass
#Converting multiline text to single line text
textString = " ".join(textList)
print(textString)
#Set language to english (en)
language = 'en'
#Call GTTS
myAudio = gTTS(text=textString, lang=language, slow=False)
#Save as mp3 file
myAudio.save("Audio.mp3")
Can anyone help me?
I have tried nothing because I could not find anything on this errors.
You asked if other X-platform libraries can better the "Robotic" voice and if you use PDF Aloud you can avoid re-inventing Pythonic Wheels use your Audacity to record the MP3. Should work with the read aloud extensions of Foxit and Chrome, here showing in Edge on Windows.

Search in PDF for error in Dimensions and highlight the error

I need a help in writing a python program for (find dimensions like 12 In. (304.8 mm) in whole multiple PDFs and should highlight the wrong existance of conversions shown in the PDFs) Also need which page the error in conversion happened or highlight the error
import PyPDF4
import re
FILE_PATH = 'C:\HB on Installation & Maintenance practices of EPM 220 mm.pdf'
with open(FILE_PATH, mode='rb') as f:
reader = PyPDF4.PdfFileReader(f)
page = reader.getPage(4)
txt = page.extractText()
location = re.findall("220 mm", txt)
print(location)

How to load multiple text file into Google text to speech?

I have been using google text to speech API because of how great the voices are. The only problem is been trying to find how to make it user friendly. The biggest thing is google text to speech can only accept text files with 5000 or fewer characters. The main issue that, I have been finding is that currently all I can do is use a single text file copy and paste my stuff on there before saving. Does anyone know how can I upload a folder filled with text files to make it quicker? Plus also saving the mp3 instead of overwriting them?
# [START tts_ssml_address_imports]
from google.cloud import texttospeech
import os
import html
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] =
# [END tts_ssml_address_imports]
# [START tts_ssml_address_audio]
def ssml_to_audio(ssml_text, outfile):
# Generates SSML text from plaintext.
#
# Given a string of SSML text and an output file name, this function
# calls the Text-to-Speech API. The API returns a synthetic audio
# version of the text, formatted according to the SSML commands. This
# function saves the synthetic audio to the designated output file.
#
# Args:
# ssml_text: string of SSML text
# outfile: string name of file under which to save audio output
#
# Returns:
# nothing
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Sets the text input to be synthesized
synthesis_input = texttospeech.types.SynthesisInput(text=ssml_text)
# Builds the voice request, selects the language code ("en-US") and
# the SSML voice gender ("MALE")
voice = texttospeech.types.VoiceSelectionParams(language_code='en-US',
name="en-US-Wavenet-D",
ssml_gender=texttospeech.enums.SsmlVoiceGender.MALE))
# Selects the type of audio file to return
audio_config = texttospeech.types.AudioConfig(audio_encoding="LINEAR16", pitch = 0, speaking_rate = 0.9)
# Performs the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(synthesis_input, voice, audio_config)
# Writes the synthetic audio to the output file.
with open(outfile, 'wb') as out:
out.write(response.audio_content)
print('Audio content written to file ' + outfile)
# [END tts_ssml_address_audio]
def main():
# test example address file
file = 'input_text.txt'
with open(file, 'r') as f:
text = f.read()
ssml_text = text
ssml_to_audio(ssml_text, 'file_output_speech.mp3')
# [END tts_ssml_address_test]
if __name__ == '__main__':
main()

how can i convert a text file to mp3 file using python pyttsx3 and sapi5?

Here is my python code..
import pyttsx3;
engine = pyttsx3.init(driverName='sapi5')
infile = "tanjil.txt"
f = open(infile, 'r')
theText = f.read()
f.close()
engine.say(theText)
engine.runAndWait()
I couldn't save the file to audio file
As of July 14 2019, I'm able to save to file with the pyttsx3 library (without using another library or internet connection).
It doesn't appear to be documented, but looking at the source code in github for the Engine class in "engine.py" (https://github.com/nateshmbhat/pyttsx3/blob/master/pyttsx3/engine.py), I was able to find a "save_to_file" function:
def save_to_file(self, text, filename, name=None):
'''
Adds an utterance to speak to the event queue.
#param text: Text to sepak
#type text: unicode
#param filename: the name of file to save.
#param name: Name to associate with this utterance. Included in
notifications about this utterance.
#type name: str
'''
self.proxy.save_to_file(text, filename, name)
I am able to use this like:
engine.save_to_file('the text I want to save as audio', path_to_save)
Not sure the format - it's some raw audio format (I guess it's maybe something like aiff) - but I can play it in an audio player.
If you install pydub:
https://pypi.org/project/pydub/
then you can easily convert this to mp3, e.g.:
from pydub import AudioSegment
AudioSegment.from_file(path_to_save).export('converted.mp3', format="mp3")
I've tried #Brian's solution but it didn't work for me.
I searched around a bit and I couldn't figure out how to save the speech to mp3 in pyttx3 but I found another solution without pyttx3.
It can take a .txt file and directly output a .wav file,
def txt_zu_wav(eingabe, ausgabe, text_aus_datei = True, geschwindigkeit = 2, Stimmenname = "Zira"):
from comtypes.client import CreateObject
engine = CreateObject("SAPI.SpVoice")
engine.rate = geschwindigkeit # von -10 bis 10
for stimme in engine.GetVoices():
if stimme.GetDescription().find(Stimmenname) >= 0:
engine.Voice = stimme
break
else:
print("Fehler Stimme nicht gefunden -> Standard wird benutzt")
if text_aus_datei:
datei = open(eingabe, 'r')
text = datei.read()
datei.close()
else:
text = eingabe
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
stream.Open(ausgabe, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
engine.speak(text)
stream.Close()
txt_zu_wav("test.txt", "test_1.wav")
txt_zu_wav("It also works with a string instead of a file path", "test_2.wav", False)
This was tested with Python 3.7.4 on Windows 10.
import pyttsx3
engine = pyttsx3.init("sapi5")
voices = engine.getProperty("voices")[0]
engine.setProperty('voice', voices)
text = 'Your Text'
engine.save_to_file(text, 'name.mp3')
engine.runAndWait() # don't forget to use this line
Try the following code snippet to convert text to audio and save it as an mp3 file.
import pyttsx3
from pydub import AudioSegment
engine = pyttsx3.init('sapi5')
engine.save_to_file('This is a test phrase.', 'test.mp3') # raw audio file
engine.runAndWait()
AudioSegment.from_file('test.mp3').export('test.mp3', format="mp3") # audio file in mp3 format
NB: pyttsx3 save_to_file() method creates a raw audio file and it won't be useful for other applications to use even if we are able to play it in the media player. pydub is a useful package to convert raw audio into a specific format.

crack pdf using python scrypt

i have to write a scrypt (for a university class and i must send to proffesor until sunday) that will crack a pdf file, i have tryied a lot so far but cant make it work my code is:
#!/usr/bin/python2.7
from PyPDF2 import PdfFileReader, PdfFileWriter
import logging
import time
t=time.localtime()
t1=time.asctime(t)
log_filename='pass_found.log'
logging.basicConfig(filename=log_filename,level=logging.INFO)
output = PdfFileWriter()
pdf = PdfFileReader(file( "sf.pdf", "rb"))
f=open('datepass.txt')
content=f.readlines()
words=[x.strip() for x in content]
for i in words:
if (pdf.decrypt(i) == 1) or (pdf.decrypt(i) == 2):
logging.info('{0} :o kodikos einai "{1}"'.format(t1,i))
datepass is a txt that contains date passwords(DD-MM-YY-> date-month-year)

Categories