-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPmupdf_table_json.py
300 lines (245 loc) · 10.1 KB
/
Pmupdf_table_json.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Input PDF for this script: https://drive.google.com/file/d/1Daa9oZJm4-b6NqUsVGxK2euemcFnf8jH/
# https://pymupdf.readthedocs.io/en/latest/page.html#Page.find_tables
# https://stackoverflow.com/questions/56155676/how-do-i-extract-a-table-from-a-pdf-file-using-pymupdf
import fitz # PyMuPDF
def update_item_ID(k, entity_idx, type, table_ID):
idx = entity_idx[k]
if type == 'Data':
# table: data row
flag = 'TD' #
elif type == 'Note':
# table: labels
flag = 'TL'
idx_list = list(idx)
idx_list[1] = flag + str(table_ID)
entity_idx[k] = tuple(idx_list)
return(entity_idx)
def detect_table(xLLM_entity):
# detect and flag simple pseudo-tables
entity_txt = xLLM_entity[0]
entity_type = xLLM_entity[1]
entity_idx = xLLM_entity[2]
table_ID = -1
table_flag = False
for k in range(1, len(entity_type)):
type = entity_type[k]
text = entity_txt[k]
old_text = entity_txt[k-1]
old_type = entity_type[k-1]
if ( (
(type == 'Data' and old_type == 'Note') or
(type == 'Note' and old_type == 'Data') or
(type == 'Data' and old_type == 'Data')
)
and old_text.count('|') == text.count('|')
and text.count('|') > 2
):
print("detected table", table_ID + 1)
if not table_flag:
table_ID += 1
table_flag = True
idx = entity_idx[k]
old_idx = entity_idx[k-1]
# update item_ID (idx[1] and old_idx[1]) in current and previous row
# item_ID starts with letter D if data, or N if labels
update_item_ID(k, entity_idx, type, table_ID)
update_item_ID(k-1, entity_idx, old_type, table_ID)
else:
#table_ID = -1
table_flag = False
return(xLLM_entity)
def cprint_page(xLLM_entity, OUT):
entity_txt = xLLM_entity[0]
entity_type = xLLM_entity[1]
entity_idx = xLLM_entity[2]
for k in range(len(entity_type)):
type = entity_type[k]
text = entity_txt[k]
text = text.strip()
text = text.replace(" ", " ")
text = text.replace(" |", "|")
text = text.replace("| ", "|")
text = text.replace("||","|")
text = text.encode('unicode-escape').decode('ascii')
text = text.replace('\\u2022', '•')
text = text.replace('\\u2013', '--')
text = text.replace('\\u2014', '--')
text = text.replace('\\u2019', "'")
text = text.replace('\\u201c', '"')
text = text.replace('\\u201d', '"')
idx = entity_idx[k]
doc_ID = idx[3]
block_ID = idx[0]
item_ID = idx[1]
sub_ID = idx[2]
pn = idx[4] # page number
fs = idx[5] # font size
fc = idx[6] # font color
ft = idx[7] # font typeface
#- print(k, type, idx, text)
OUT.write(f"{type:<8}{block_ID:>3}{item_ID:>5}{sub_ID:>3}{pn:>3}"
f"{fs:>5}{fc:>9} {ft:<20}{text:<80}\n")
OUT.write("\n")
return()
def update_page(text, type, entity, idx):
entity_txt = entity[0]
entity_type = entity[1]
entity_idx = entity[2]
block_ID = idx[0]
item_ID = idx[1]
sub_ID = idx[2]
k = len(entity_txt)
if k > 0:
old_type = entity_type[k-1]
old_idx = entity_idx[k-1]
old_block_ID = old_idx[0]
old_item_ID = old_idx[1]
old_sub_ID = old_idx[2]
else:
old_type = ""
old_block_ID = ""
old_item_ID = ""
old_sub_ID = ""
if type in ('Note', 'Data'):
sep = "|"
else:
sep = " "
if (type == old_type and block_ID == old_block_ID
and item_ID == old_item_ID and sub_ID == old_sub_ID):
new_text = entity_txt[k-1] + text + sep
entity_txt[k-1] = new_text
else:
entity_txt.append(sep + text + sep)
entity_type.append(type)
entity_idx.append(idx)
return(entity)
def convert_pdf_to_json(pdf_path, json_path, doc_ID, text_path):
# Open the PDF file
pdf_document = fitz.open(pdf_path)
content = ""
# Iterate through the pages
for page_num in range(len(pdf_document)):
OUT.write("\n----------------------------\n")
OUT.write("Processing page " + str(page_num) + "\n\n")
print("Page:", page_num)
page = pdf_document.load_page(page_num)
text_data = page.get_text("dict") # also extract as "json" to get tokens in green font
tabs = page.find_tables()
for tabs_index, tab in enumerate(tabs):
# iterate over all tables
index = (page_num, tabs_index)
table_data = tab.extract() # extracting tabs[i], the i-th table in this page
if len(table_data) > 0:
# if not, ignore this table (note the important parameter threshold here)
OUT.write("Table " + str(index) + ":\n")
for row in table_data:
OUT.write(str(row) + "\n")
OUT.write("\n")
itemize = False
item_ID = -1
sub_ID = -1
block_ID = -1
item = ""
fsm = -1 # top level font size in bullet list
fst = 64 # min title font size (top parameter)
title = ""
notes = ""
old_block_number = -1
old_font_size = -1
entity_txt = []
entity_idx = []
entity_type = []
type = ""
old_text = ""
old_type = ""
for block in text_data["blocks"]:
if block["type"] == 0: # Text block
block_number = block["number"]
for line in block["lines"]:
for span in line["spans"]:
text = span["text"]
font_name = span["font"]
font_size = span["size"]
font_size = round(font_size,1)
font_color = span["color"]
if font_size > fst:
type = 'Title'
elif ord(text[0]) == 8226:
itemize = True
if fsm == -1:
fsm = font_size
if font_size > 0.98 * fsm:
item_ID += 1
type = 'List'
else:
sub_ID +=1
type = 'SubList'
elif itemize:
#- itemize = ((0.99 < font_size/old_font_size < 1.01) and
#- (not text[0].isupper() or ord(old_text[0]) == 8226))
itemize = ((0.99 < font_size/old_font_size < 1.01) or
(ord(old_text[0]) == 8226))
if not itemize:
item_ID = -1
sub_ID = -1
block_ID += 1
type = 'Note'
else:
if not text[0].isdigit() and text[0] not in ('$', '+', '-'):
type = 'Note'
#- elif block_number != old_block_number:
else:
type = 'Data'
if block_ID == -1:
block_ID += 1
elif ((type not in (old_type, 'List', 'SubList')) or
(not (0.99 < font_size/old_font_size < 1.01))):
if (old_text != "" and ord(old_text[0]) != 8226 and
type not in ('List', 'SubList')):
block_ID += 1
idx = (block_ID, item_ID, sub_ID, doc_ID, page_num, font_size,
font_color, font_name, block_number)
entity = (entity_txt, entity_type, entity_idx)
update_page(text, type, entity, idx)
old_font_size = font_size
old_text = text
old_type = type
old_block_number = block_number
entity = detect_table(entity)
cprint_page(entity, OUT)
image_list = page.get_images()
for image_index, img in enumerate(image_list, start=1):
xref = img[0]
base_image = pdf_document.extract_image(xref)
image_bytes = base_image["image"]
size = len(image_bytes)
ext = base_image["ext"]
index = (page_num, image_index)
OUT.write(f"Image {str(index):<8}{str(ext):>5} size = {str(size):>6}\n")
#- with open(f"image_{page_num + 1}_{image_index}.{ext}", "wb") as image_file:
#- image_file.write(image_bytes)
content += "__P" + str(page_num) + "\n" + page.get_text("json") # "text", "html", "json"
# Write the json content to a file
with open(json_path, 'w', encoding='utf-8') as json_file:
json_file.write(content)
# --- Main ---
doc_ID = 0 # to identify the PDF doc
filename = 'nvda-f2q24-investor-presentation-final-1'
pdf_path = filename + '.pdf'
json_path = filename + '.json'
text_path = filename + '.txt'
OUT = open(text_path, "wt", encoding="utf-8")
convert_pdf_to_json(pdf_path, json_path, doc_ID, OUT)
OUT.close()
# --- PDF to Images ---
from PIL import Image
pdf_document = fitz.open(pdf_path)
zoom = 2 # to increase the resolution
mat = fitz.Matrix(zoom, zoom)
for page_num in range(len(pdf_document)):
page = pdf_document.load_page(page_num)
pix = page.get_pixmap(matrix = mat) # or (dpi = 300)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
filename = "PDF" + str(page_num) + '.png' # you could change image format accordingly
img.save(filename)
print('Converting PDFs to Image ... ' + filename)