-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (34 loc) · 1.23 KB
/
main.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 18 12:51:14 2020
@author: apurv
"""
import os
from os import listdir
from os.path import isfile, join
import pdfplumber
import pyttsx3
##Setting the current working directory
os.chdir("C://Users//apurv//OneDrive//Documents//Projects//2020//pdf-to-audiofile")
pdf_path = 'pdf_files//'
audio_path = 'audio_files//'
##Getting list of all files in pdf folder
filenames = [f for f in listdir(pdf_path) if isfile(join(pdf_path, f))]
## Setting the voice properties
#initialize the voice engine
engine = pyttsx3.init()
##Choosing voice type
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id) #0 for Male, 1 for Female
##Choosing voice rate
rate = engine.getProperty('rate')
engine.setProperty('rate', 200)
##Reading all PDF files one by one and saving the audio files for respective files
for f in filenames:
with pdfplumber.open(r''.join([pdf_path,f])) as pdf:
text = ''
for page in pdf.pages:
text = text + str(' ' if page.extract_text() is None else page.extract_text())
text.replace("_"," ").replace(" \n"," ")
engine.save_to_file(text, "".join([audio_path,f.split('.pdf')[0],'.mp3']))
engine.runAndWait()