-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethodAWSpdf.py
More file actions
148 lines (116 loc) · 4.63 KB
/
Copy pathmethodAWSpdf.py
File metadata and controls
148 lines (116 loc) · 4.63 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import boto3
import os
import time
import csv
from csvConcatenation import concatenate_csv_files
textract = boto3.client("textract")
s3 = boto3.client("s3")
S3_BUCKET = "transcriptmiha"
def upload_pdf_to_s3(file_path, bucket, object_name):
with open(file_path, "rb") as file_data:
s3.upload_fileobj(file_data, bucket, object_name)
return object_name
def start_textract_job(bucket, document_name):
response = textract.start_document_analysis(
DocumentLocation={"S3Object": {"Bucket": bucket, "Name": document_name}},
FeatureTypes=["TABLES"],
)
return response["JobId"]
def wait_for_job(job_id):
while True:
response = textract.get_document_analysis(JobId=job_id)
status = response["JobStatus"]
if status == "SUCCEEDED":
return
elif status == "FAILED":
raise Exception("Textract job failed.")
time.sleep(5)
def get_full_textract_result(job_id):
pages = []
next_token = None
while True:
if next_token:
response = textract.get_document_analysis(
JobId=job_id, NextToken=next_token
)
else:
response = textract.get_document_analysis(JobId=job_id)
pages.append(response)
next_token = response.get("NextToken")
if not next_token:
break
return pages
def extract_tables_from_response(response):
blocks = response["Blocks"]
tables = [block for block in blocks if block["BlockType"] == "TABLE"]
extracted_tables = []
for table in tables:
rows = {}
for relationship in table.get("Relationships", []):
if relationship["Type"] == "CHILD":
for child_id in relationship["Ids"]:
cell = next(
(block for block in blocks if block["Id"] == child_id), None
)
if cell and cell["BlockType"] == "CELL":
row_idx = cell["RowIndex"]
col_idx = cell["ColumnIndex"]
if row_idx not in rows:
rows[row_idx] = {}
rows[row_idx][col_idx] = cell
table_data = []
max_col_index = max((max(row.keys()) for row in rows.values()), default=0)
for row_idx in sorted(rows.keys()):
row_data = []
for col_idx in range(1, max_col_index + 1):
text = ""
cell = rows[row_idx].get(col_idx)
if cell:
for rel in cell.get("Relationships", []):
if rel["Type"] == "CHILD":
for child_id in rel["Ids"]:
word = next(
(
b
for b in blocks
if b["Id"] == child_id
and b["BlockType"] == "WORD"
),
None,
)
if word:
text += word["Text"] + " "
row_data.append(text.strip())
table_data.append(row_data)
extracted_tables.append(table_data)
return extracted_tables
def save_table_to_csv(table, csv_filename):
with open(csv_filename, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
for row in table:
writer.writerow(row)
def process_pdfs_in_folder(pdf_path, output_folder, delete_after=True):
filename = os.path.basename(pdf_path)
name_no_ext = os.path.splitext(filename)[0]
os.makedirs(output_folder, exist_ok=True)
object_name = f"textract_uploads/{filename}"
upload_pdf_to_s3(pdf_path + ".pdf", S3_BUCKET, object_name)
print(f"Uploaded {filename} to S3. Starting Textract analysis...")
job_id = start_textract_job(S3_BUCKET, object_name)
wait_for_job(job_id)
responses = get_full_textract_result(job_id)
table_count = 1
for response in responses:
tables = extract_tables_from_response(response)
for table in tables:
csv_filename = os.path.join(
output_folder, f"{name_no_ext}_table_{table_count}.csv"
)
save_table_to_csv(table, csv_filename)
print(f"Saved table {table_count} to {csv_filename}")
table_count += 1
# detele from aws
if delete_after:
print(f"Deleting {object_name} from S3...")
s3.delete_object(Bucket=S3_BUCKET, Key=object_name)
print("Deleted.")