-
Notifications
You must be signed in to change notification settings - Fork 39
/
approx_time_to_read_pdf.py
80 lines (74 loc) · 2.27 KB
/
approx_time_to_read_pdf.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
import os
import sys
import re
import time
import PyPDF2
def getPageCount(pdf_file):
pdfFileObj = open(pdf_file, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pages = pdfReader.numPages
return pages
def extractData(pdf_file, page):
pdfFileObj = open(pdf_file, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(page)
data = pageObj.extractText()
return data
def getWordCount(data):
data=data.split()
return len(data)
def calculateSpeed():
print (
"""There had been some speculation on the dangers of landing
some hours before. The planetary target was a huge one for an
oxygen-water world. Though it lacked the size of the
uninhabitable hydrogen-ammonia planets and its low density
made its surface gravity fairly normal, its gravitational forces fell
off but slowly with distance. In short, its gravitational potential
was high and the ship's Calculator was a run-of-the-mill model
not designed to plot landing trajectories at that potential range.
That meant the Pilot would have to use manual controls.
""")
t0=time.time()
inp=input()
if inp=="ok":
t1 = time.time()
totalTime = t1-t0
else: print("\nYou had to type \"ok\" before pressing Enter!!\n")
speed = int(100/totalTime)
return speed
def main():
if len(sys.argv)!=2:
print('command usage: python know_count.py FileName')
exit(1)
else:
pdfFile = sys.argv[1]
# check if the specified file exists or not
try:
if os.path.exists(pdfFile):
print("\nfile found!\n")
except OSError as err:
print(err.reason)
exit(1)
# get the word count in the pdf file
print("Loading pdf...\n")
totalWords = 0
numPages = getPageCount(pdfFile)
for i in range(numPages):
text = extractData(pdfFile, i)
totalWords+=getWordCount(text)
time.sleep(1)
#print (totalWords)
print ("PDF loaded\n")
print("\nType \"ok\" and press Enter after you complete reading the paragraph displayed\n")
speed=calculateSpeed()
seconds = totalWords/speed
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
if hours>1:
print("You will take approximately %d hours to complete the book\n" % (hours+1))
else:
print("You will take approximately %d minutes to complete the book\n" % (minutes+1))
if __name__ == '__main__':
main()