0

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 library. If there is a better library 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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
liam
  • 11
  • 1
  • Please do not offload the errors to another site, include them in your answers. The error messages show that you script is not able to connect to Google's server. Maybe you need to upgrade the library (`pip3 install -U gTTS`) or it is just a temporary problem, or the connection is being refused because it requires authentication. Have you tried using the `--debug` flag together with the `gtts-cli`-command? – Daniel F Dec 04 '22 at 17:36
  • I tried to use gtts-cli --debug -f log.txt. When I did this I got this error: gtts.tts - DEBUG - HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: /_/TranslateWebserverUi/data/batchexecute (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused')) . I also tried pip3 install -U gTTS and this also did not work. I am sorry about offloading the error. I know for next time. – liam Dec 04 '22 at 18:11
  • What does `python3 -c "import requests; r = requests.get('https://translate.google.com'); print(r.status_code, r.content[:16])"` return on the machine where you're executing your script? It should return `200 b' <'` – Daniel F Dec 04 '22 at 18:29

1 Answers1

0

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.

enter image description here

K J
  • 8,045
  • 3
  • 14
  • 36