-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfWordCounter.py
More file actions
52 lines (43 loc) · 1.61 KB
/
pdfWordCounter.py
File metadata and controls
52 lines (43 loc) · 1.61 KB
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
import PyPDF2
import sys
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QPushButton, QGridLayout, QLineEdit
def wordCounter(pdf, pageStart, pageEnd):
"""
Description
-----------
wordCounter() counts the number of words in a given pdf between a given interval of
Parameters
----------
pdf : string
Path to the PDF in question
pageStart : int
Starting page number
pageEnd : int
Ending page number
Returns
-------
wordCount : int
Total number of words in the interval [pageStart, pageEnd]
"""
pdf = PyPDF2.PdfReader(pdf)
# Declare start and end pages, and initialize the word count varialbe
wordCount = 0
for i in range(pageStart-1, pageEnd-1): # Throw on the -1 on pageStart and pageEnd since indexing begins at 0. So page 10 would really be page 9 in the eyes of the program
content = pdf.pages[i].extract_text()
wordCount += len(content.split())
# The counting function counts page numbers, this removes that redundancy by subtracting off the number of pages from the final word count
wordCount -= len(range(pageStart,pageEnd))
return(wordCount)
# Initialize the application and the window
app = QApplication([])
window = QWidget()
window.setWindowTitle("PDF Word Counter")
window.setGeometry(700, 400, 500, 300)
# Create the rest of the application
layout=QGridLayout()
# layout.addWidget(QLabel("Starting Page"), 0,0)
layout.addWidget(QLineEdit('Default Value', self),0,0)
# helloMsg = QLabel("<h1>Hello, World!</h1>", parent=window)
# helloMsg.move(60, 15)
window.show()
sys.exit(app.exec())