Template request | Bug report | Generate Data Product
Tags: #pdf #text2audio #snippet #operations #mp3
Author: Sanjay Sabu
Description: This notebook converts PDF documents into MP3 audio files.
!pip install pdfminer.six
!pip install gTTS
from io import StringIO
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfparser import PDFParser
from gtts import gTTS
def convert_pdf_to_string(file_path):
output_string = StringIO()
with open(file_path, "rb") as in_file:
parser = PDFParser(in_file)
doc = PDFDocument(parser)
rsrcmgr = PDFResourceManager()
device = TextConverter(rsrcmgr, output_string, laparams=LAParams())
interpreter = PDFPageInterpreter(rsrcmgr, device)
for page in PDFPage.create_pages(doc):
interpreter.process_page(page)
return output_string.getvalue()
def convert_title_to_filename(title):
filename = title.lower()
filename = filename.replace(" ", "_")
return filename
def split_to_title_and_pagenum(table_of_contents_entry):
title_and_pagenum = table_of_contents_entry.strip()
title = None
pagenum = None
if len(title_and_pagenum) > 0:
if title_and_pagenum[-1].isdigit():
i = -2
while title_and_pagenum[i].isdigit():
i -= 1
title = title_and_pagenum[:i].strip()
pagenum = int(title_and_pagenum[i:].strip())
return title, pagenum
pdf_name = "Installation_Guide.pdf" # .pdf file you want to convert
print(convert_pdf_to_string(pdf_name))
rr = convert_pdf_to_string(pdf_name)
string_of_text = ""
for text in rr:
string_of_text += text
final_file = gTTS(text=string_of_text, lang="en") # store file in variable
final_file.save("Generated Speech.mp3") # save file to computer