-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPdfToAudio.py
More file actions
29 lines (22 loc) · 881 Bytes
/
Copy pathPdfToAudio.py
File metadata and controls
29 lines (22 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from PyPDF2 import PdfReader
from gtts import gTTS
import os
pdf_path = '/Users/tanishjain/Desktop/Adi/DOC/Bitcoin-satoshiNakamoto.pdf'
# Open the PDF file
with open(pdf_path, 'rb') as pdf_file:
# Create a PdfReader object
pdf_reader = PdfReader(pdf_file)
# Initialize text variable to store extracted text from all pages
all_text = ""
# Iterate through all pages and extract text
for page_num in range(len(pdf_reader.pages)):
text = pdf_reader.pages[page_num].extract_text()
clean_text = text.strip().replace('\n', ' ')
all_text += clean_text
# Using gTTS to convert text to speech
speech = gTTS(text=all_text, lang='en', slow=False)
# Save the speech to an MP3 file
mp3_path = '/Users/tanishjain/Desktop/Adi/DOC/Bitcoin-satoshiNakamoto.mp3'
speech.save(mp3_path)
# Play the MP3 file (optional)
os.system("open " + mp3_path)