-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfExtract.py
More file actions
93 lines (69 loc) · 2.52 KB
/
pdfExtract.py
File metadata and controls
93 lines (69 loc) · 2.52 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
82
83
84
85
86
87
88
89
90
91
92
93
import os
import time
from PIL import Image
from pdf2image import convert_from_path
from pytesseract import pytesseract
def download_wait(directory, timeout, nfiles=None):
"""
Wait for downloads to finish with a specified timeout.
Args
----
directory : str
The path to the folder where the files will be downloaded.
timeout : int
How many seconds to wait until timing out.
nfiles : int, defaults to None
If provided, also wait for the expected number of files.
"""
seconds = 0
dl_wait = True
while dl_wait and seconds < timeout:
time.sleep(1)
dl_wait = False
files = os.listdir(directory)
if nfiles and len(files) != nfiles:
dl_wait = True
for fname in files:
if fname.endswith('.crdownload'):
dl_wait = True
seconds += 1
return seconds
def iterate_and_extract(pdf_dir):
"""
Iterates through a directory of PDF files, extracts text patterns, and writes them to a CSV file.
"""
v = 1
for filename in os.listdir(pdf_dir):
if filename.endswith('.pdf'):
pdf_file_path = os.path.join(pdf_dir, filename)
# Store Pdf with convert_from_path function
images = convert_from_path(pdf_file_path)
for i in range(len(images)):
# Save pages as images in the pdf
images[i].save('pages/' + filename.replace('.pdf', '') + '_page' + str(i) + '.jpg', 'JPEG')
v += 1
def capture_and_extract_image(image):
# Define path to tessaract.exe
path_to_tesseract = r'C:/Program Files/Tesseract-OCR/tesseract.exe'
# Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract
# Open image with PIL
img = Image.open(image)
# Extract text from image
text = pytesseract.image_to_string(img)
print(text)
def capture_and_extract_images(images):
# Define path to tessaract.exe
path_to_tesseract = r'C:/Program Files/Tesseract-OCR/tesseract.exe'
# Define path to images folder
# Point tessaract_cmd to tessaract.exe
pytesseract.tesseract_cmd = path_to_tesseract
# Get the file names in the directory
for root, dirs, file_names in os.walk(images):
# Iterate over each file name in the folder
for file_name in file_names:
# Open image with PIL
img = Image.open(images + file_name)
# Extract text from image
text = pytesseract.image_to_string(img)
print(text)