-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_use.py
More file actions
81 lines (62 loc) · 1.81 KB
/
Copy pathpdf_use.py
File metadata and controls
81 lines (62 loc) · 1.81 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
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
81
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Class that simpifies the reading of PDFs.
"""
import PyPDF2
import re
class PDFUse(object):
"""Class that simpifies the reading of PDFs."""
def __init__(self, filename):
super(PDFUse, self).__init__()
self.filename = filename
self.init()
def init(self):
"""
Generates the object and decrypts.
"""
self.pdf = PyPDF2.PdfFileReader(self.filename)
if self.pdf.isEncrypted:
self.pdf.decrypt('')
def is_valid(self):
"""
Does an operation to know if the PDF file can be use with
this module.
"""
try:
self.pdf.getDocumentInfo()
result = True
except:
result = False
return result
def get_number_of_pages(self):
"""
Returns the total amount of pages.
"""
return self.pdf.getNumPages()
def get_info(self):
"""
Returns the base info of the document, if it has.
"""
return self.pdf.getDocumentInfo()
def get_fields(self):
"""
Returns interactive content.
"""
return self.pdf.getFields()
def get_content(self, page_num, regex=None):
"""
Returns the content of the page.
- If regex != None, returns compatible content with
the regular expression.
"""
page_content = self.pdf.getPage(page_num).extractText()
if regex:
page_content = re.findall(regex, page_content)
return page_content
def main():
filename = "book.pdf"
pdf_class = PDFUse(filename)
print(pdf_class.get_content(100, r".*"))
if __name__ == "__main__":
main()