0

I'm a novice python user so I apologize if my questions are simplistic or stupid. I've tried Google searching before posting, I promise.

I have a very large document (roughly 1100 pages) that I need a speech to text MP3 of. All of the TTS modules I've seen (GTTS, etc) want strings, not documents. Here are my core questions:

  • Can this be done in one go? Do I have to break up the document into smaller chunks?

  • Is Python the correct tool for the job?

  • In the case of the code below, is there a way to replace the Myfile = string with something like Myfile = open(mydoc.txt)?

(Yes I just copy-pasted this from the web, but I promise I've been playing with it on my own.)

# to speech conversion 
from gtts import gTTS 

# This module is imported so that we can  
# play the converted audio 
import os 

# The text that you want to convert to audio 
mytext = 'Welcome to geeksforgeeks!'

# Language in which you want to convert 
language = 'en'

# Passing the text and language to the engine,  
# here we have marked slow=False. Which tells  
# the module that the converted audio should  
# have a high speed 
myobj = gTTS(text=mytext, lang=language, slow=False) 

# Saving the converted audio in a mp3 file named 
# welcome  
myobj.save("welcome.mp3") 


Tooluser
  • 55
  • 5
  • you can read the 1100 pages document easily in python, by implementation wise it all depends on you how to implement it like in one go or in chunk – sahasrara62 Apr 20 '20 at 15:51
  • What kind of file is your document? – Boris Verkhovskiy Apr 20 '20 at 16:33
  • If it's a pdf (or some other non-plain text file), you need to google how to extract text from a PDF with Python (which I'm sure is a pain, and you'll almost certainly want to extract that text to a .txt file and then have to manually edit that `.txt` file to clean it up. You should try to ask whoever sent you the document if they could send you a txt, that'll make your task pretty trivial), then you can `with open("that_txt_file.txt") as f: text = f.read()` and then the variable `text` will contain your whole file as a string and you can pass it to `gTTS` as in the code sample you pasted. – Boris Verkhovskiy Apr 20 '20 at 16:36
  • With GTTS, you'll almost certainly have to break up the text into chunks, I don't think they're going to send you gigabytes of audio for one request. – Boris Verkhovskiy Apr 20 '20 at 16:41
  • If it's just a txt, then look at this question: https://stackoverflow.com/questions/7409780/reading-entire-file-in-python – Boris Verkhovskiy Apr 20 '20 at 16:44

1 Answers1

0
filepath = 'test.txt'
text = ''
with open(filepath) as fp:
    line = fp.readline()
    while line:
        text += line.strip() + '\n'
        line = fp.readline()

The code here is reading the text file line by line and then appending it to the 'text' string variable. From there you should be able to load it into gTTS. It might take some time to load or have a large file size, but it should work.

Holden
  • 632
  • 4
  • 15
  • Thank you, Holden! This runs without errors, but its either taking forever or..... Not working. Any idea how I could tell if its actually running? – Tooluser Apr 20 '20 at 18:27
  • You could throw a print statement into the loop to see if it is being read. If it's getting stuck at the part where you turn it into the mp3... the file will be very large so it could take a very long time. Maybe try leaving it on over night? If none of these are working it might be worth it to look at the suggestions other people had. I think breaking it up into chunks is a good way to go as well. – Holden Apr 20 '20 at 19:13
  • I ended up breaking the file into 30 (!) chunks which did the trick. Thank you again! – Tooluser Apr 20 '20 at 23:06