magic-pdf 0.9.3__py3-none-any.whl → 0.10.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- magic_pdf/config/constants.py +53 -0
- magic_pdf/config/drop_reason.py +35 -0
- magic_pdf/config/drop_tag.py +19 -0
- magic_pdf/config/make_content_config.py +11 -0
- magic_pdf/{libs/ModelBlockTypeEnum.py → config/model_block_type.py} +2 -1
- magic_pdf/data/data_reader_writer/filebase.py +3 -0
- magic_pdf/data/read_api.py +1 -1
- magic_pdf/dict2md/mkcontent.py +226 -185
- magic_pdf/dict2md/ocr_mkcontent.py +11 -11
- magic_pdf/filter/pdf_meta_scan.py +101 -79
- magic_pdf/integrations/rag/utils.py +4 -5
- magic_pdf/libs/config_reader.py +5 -5
- magic_pdf/libs/draw_bbox.py +3 -2
- magic_pdf/libs/pdf_image_tools.py +36 -12
- magic_pdf/libs/version.py +1 -1
- magic_pdf/model/doc_analyze_by_custom_model.py +2 -0
- magic_pdf/model/magic_model.py +13 -13
- magic_pdf/model/pdf_extract_kit.py +122 -76
- magic_pdf/model/sub_modules/model_init.py +40 -35
- magic_pdf/model/sub_modules/ocr/paddleocr/ocr_utils.py +33 -7
- magic_pdf/model/sub_modules/ocr/paddleocr/ppocr_273_mod.py +12 -4
- magic_pdf/model/sub_modules/table/rapidtable/rapid_table.py +2 -0
- magic_pdf/model/sub_modules/table/tablemaster/tablemaster_paddle.py +30 -28
- magic_pdf/para/para_split.py +411 -248
- magic_pdf/para/para_split_v2.py +352 -182
- magic_pdf/para/para_split_v3.py +110 -53
- magic_pdf/pdf_parse_by_ocr.py +2 -0
- magic_pdf/pdf_parse_by_txt.py +2 -0
- magic_pdf/pdf_parse_union_core.py +174 -100
- magic_pdf/pdf_parse_union_core_v2.py +202 -36
- magic_pdf/pipe/AbsPipe.py +28 -44
- magic_pdf/pipe/OCRPipe.py +5 -5
- magic_pdf/pipe/TXTPipe.py +5 -6
- magic_pdf/pipe/UNIPipe.py +24 -25
- magic_pdf/post_proc/pdf_post_filter.py +7 -14
- magic_pdf/pre_proc/cut_image.py +9 -11
- magic_pdf/pre_proc/equations_replace.py +203 -212
- magic_pdf/pre_proc/ocr_detect_all_bboxes.py +235 -49
- magic_pdf/pre_proc/ocr_dict_merge.py +5 -5
- magic_pdf/pre_proc/ocr_span_list_modify.py +122 -63
- magic_pdf/pre_proc/pdf_pre_filter.py +37 -33
- magic_pdf/pre_proc/remove_bbox_overlap.py +20 -18
- magic_pdf/pre_proc/remove_colored_strip_bbox.py +36 -14
- magic_pdf/pre_proc/remove_footer_header.py +2 -5
- magic_pdf/pre_proc/remove_rotate_bbox.py +111 -63
- magic_pdf/pre_proc/resolve_bbox_conflict.py +10 -17
- magic_pdf/spark/spark_api.py +15 -17
- magic_pdf/tools/cli.py +3 -4
- magic_pdf/tools/cli_dev.py +6 -9
- magic_pdf/tools/common.py +26 -36
- magic_pdf/user_api.py +29 -38
- {magic_pdf-0.9.3.dist-info → magic_pdf-0.10.1.dist-info}/METADATA +11 -12
- {magic_pdf-0.9.3.dist-info → magic_pdf-0.10.1.dist-info}/RECORD +58 -59
- {magic_pdf-0.9.3.dist-info → magic_pdf-0.10.1.dist-info}/WHEEL +1 -1
- magic_pdf/libs/Constants.py +0 -55
- magic_pdf/libs/MakeContentConfig.py +0 -11
- magic_pdf/libs/drop_reason.py +0 -27
- magic_pdf/libs/drop_tag.py +0 -19
- magic_pdf/para/para_pipeline.py +0 -297
- /magic_pdf/{libs → config}/ocr_content_type.py +0 -0
- {magic_pdf-0.9.3.dist-info → magic_pdf-0.10.1.dist-info}/LICENSE.md +0 -0
- {magic_pdf-0.9.3.dist-info → magic_pdf-0.10.1.dist-info}/entry_points.txt +0 -0
- {magic_pdf-0.9.3.dist-info → magic_pdf-0.10.1.dist-info}/top_level.txt +0 -0
@@ -2,38 +2,47 @@ import time
|
|
2
2
|
|
3
3
|
from loguru import logger
|
4
4
|
|
5
|
+
from magic_pdf.config.drop_reason import DropReason
|
6
|
+
from magic_pdf.config.ocr_content_type import ContentType
|
7
|
+
from magic_pdf.layout.layout_sort import (LAYOUT_UNPROC, get_bboxes_layout,
|
8
|
+
get_columns_cnt_of_layout)
|
5
9
|
from magic_pdf.libs.commons import fitz, get_delta_time
|
6
|
-
from magic_pdf.layout.layout_sort import get_bboxes_layout, LAYOUT_UNPROC, get_columns_cnt_of_layout
|
7
10
|
from magic_pdf.libs.convert_utils import dict_to_list
|
8
|
-
from magic_pdf.libs.drop_reason import DropReason
|
9
11
|
from magic_pdf.libs.hash_utils import compute_md5
|
10
12
|
from magic_pdf.libs.local_math import float_equal
|
11
|
-
from magic_pdf.libs.ocr_content_type import ContentType
|
12
13
|
from magic_pdf.model.magic_model import MagicModel
|
13
14
|
from magic_pdf.para.para_split_v2 import para_split
|
14
15
|
from magic_pdf.pre_proc.citationmarker_remove import remove_citation_marker
|
15
|
-
from magic_pdf.pre_proc.construct_page_dict import
|
16
|
+
from magic_pdf.pre_proc.construct_page_dict import \
|
17
|
+
ocr_construct_page_component_v2
|
16
18
|
from magic_pdf.pre_proc.cut_image import ocr_cut_image_and_table
|
17
|
-
from magic_pdf.pre_proc.equations_replace import
|
18
|
-
combine_chars_to_pymudict
|
19
|
-
|
20
|
-
from magic_pdf.pre_proc.
|
21
|
-
|
22
|
-
from magic_pdf.pre_proc.
|
23
|
-
|
24
|
-
|
19
|
+
from magic_pdf.pre_proc.equations_replace import (
|
20
|
+
combine_chars_to_pymudict, remove_chars_in_text_blocks,
|
21
|
+
replace_equations_in_textblock)
|
22
|
+
from magic_pdf.pre_proc.ocr_detect_all_bboxes import \
|
23
|
+
ocr_prepare_bboxes_for_layout_split
|
24
|
+
from magic_pdf.pre_proc.ocr_dict_merge import (fill_spans_in_blocks,
|
25
|
+
fix_block_spans,
|
26
|
+
fix_discarded_block,
|
27
|
+
sort_blocks_by_layout)
|
28
|
+
from magic_pdf.pre_proc.ocr_span_list_modify import (
|
29
|
+
get_qa_need_list_v2, remove_overlaps_low_confidence_spans,
|
30
|
+
remove_overlaps_min_spans)
|
31
|
+
from magic_pdf.pre_proc.resolve_bbox_conflict import \
|
32
|
+
check_useful_block_horizontal_overlap
|
25
33
|
|
26
34
|
|
27
35
|
def remove_horizontal_overlap_block_which_smaller(all_bboxes):
|
28
36
|
useful_blocks = []
|
29
37
|
for bbox in all_bboxes:
|
30
|
-
useful_blocks.append({
|
31
|
-
|
32
|
-
|
33
|
-
|
38
|
+
useful_blocks.append({'bbox': bbox[:4]})
|
39
|
+
is_useful_block_horz_overlap, smaller_bbox, bigger_bbox = (
|
40
|
+
check_useful_block_horizontal_overlap(useful_blocks)
|
41
|
+
)
|
34
42
|
if is_useful_block_horz_overlap:
|
35
43
|
logger.warning(
|
36
|
-
f
|
44
|
+
f'skip this page, reason: {DropReason.USEFUL_BLOCK_HOR_OVERLAP}, smaller bbox is {smaller_bbox}, bigger bbox is {bigger_bbox}'
|
45
|
+
)
|
37
46
|
for bbox in all_bboxes.copy():
|
38
47
|
if smaller_bbox == bbox[:4]:
|
39
48
|
all_bboxes.remove(bbox)
|
@@ -41,27 +50,27 @@ def remove_horizontal_overlap_block_which_smaller(all_bboxes):
|
|
41
50
|
return is_useful_block_horz_overlap, all_bboxes
|
42
51
|
|
43
52
|
|
44
|
-
def __replace_STX_ETX(text_str:str):
|
45
|
-
"""
|
46
|
-
Drawback: This issue is only observed in English text; it has not been found in Chinese text so far.
|
53
|
+
def __replace_STX_ETX(text_str: str):
|
54
|
+
"""Replace \u0002 and \u0003, as these characters become garbled when extracted using pymupdf. In fact, they were originally quotation marks.
|
55
|
+
Drawback: This issue is only observed in English text; it has not been found in Chinese text so far.
|
47
56
|
|
48
|
-
|
49
|
-
|
57
|
+
Args:
|
58
|
+
text_str (str): raw text
|
50
59
|
|
51
|
-
|
52
|
-
|
60
|
+
Returns:
|
61
|
+
_type_: replaced text
|
53
62
|
"""
|
54
63
|
if text_str:
|
55
64
|
s = text_str.replace('\u0002', "'")
|
56
|
-
s = s.replace(
|
65
|
+
s = s.replace('\u0003', "'")
|
57
66
|
return s
|
58
67
|
return text_str
|
59
68
|
|
60
69
|
|
61
70
|
def txt_spans_extract(pdf_page, inline_equations, interline_equations):
|
62
|
-
text_raw_blocks = pdf_page.get_text(
|
63
|
-
char_level_text_blocks = pdf_page.get_text(
|
64
|
-
|
71
|
+
text_raw_blocks = pdf_page.get_text('dict', flags=fitz.TEXTFLAGS_TEXT)['blocks']
|
72
|
+
char_level_text_blocks = pdf_page.get_text('rawdict', flags=fitz.TEXTFLAGS_TEXT)[
|
73
|
+
'blocks'
|
65
74
|
]
|
66
75
|
text_blocks = combine_chars_to_pymudict(text_raw_blocks, char_level_text_blocks)
|
67
76
|
text_blocks = replace_equations_in_textblock(
|
@@ -71,189 +80,254 @@ def txt_spans_extract(pdf_page, inline_equations, interline_equations):
|
|
71
80
|
text_blocks = remove_chars_in_text_blocks(text_blocks)
|
72
81
|
spans = []
|
73
82
|
for v in text_blocks:
|
74
|
-
for line in v[
|
75
|
-
for span in line[
|
76
|
-
bbox = span[
|
83
|
+
for line in v['lines']:
|
84
|
+
for span in line['spans']:
|
85
|
+
bbox = span['bbox']
|
77
86
|
if float_equal(bbox[0], bbox[2]) or float_equal(bbox[1], bbox[3]):
|
78
87
|
continue
|
79
|
-
if span.get('type') not in (
|
88
|
+
if span.get('type') not in (
|
89
|
+
ContentType.InlineEquation,
|
90
|
+
ContentType.InterlineEquation,
|
91
|
+
):
|
80
92
|
spans.append(
|
81
93
|
{
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
94
|
+
'bbox': list(span['bbox']),
|
95
|
+
'content': __replace_STX_ETX(span['text']),
|
96
|
+
'type': ContentType.Text,
|
97
|
+
'score': 1.0,
|
86
98
|
}
|
87
99
|
)
|
88
100
|
return spans
|
89
101
|
|
90
102
|
|
91
103
|
def replace_text_span(pymu_spans, ocr_spans):
|
92
|
-
return list(filter(lambda x: x[
|
104
|
+
return list(filter(lambda x: x['type'] != ContentType.Text, ocr_spans)) + pymu_spans
|
93
105
|
|
94
106
|
|
95
|
-
def parse_page_core(
|
107
|
+
def parse_page_core(
|
108
|
+
pdf_docs, magic_model, page_id, pdf_bytes_md5, imageWriter, parse_mode
|
109
|
+
):
|
96
110
|
need_drop = False
|
97
111
|
drop_reason = []
|
98
112
|
|
99
|
-
|
113
|
+
"""从magic_model对象中获取后面会用到的区块信息"""
|
100
114
|
img_blocks = magic_model.get_imgs(page_id)
|
101
115
|
table_blocks = magic_model.get_tables(page_id)
|
102
116
|
discarded_blocks = magic_model.get_discarded(page_id)
|
103
117
|
text_blocks = magic_model.get_text_blocks(page_id)
|
104
118
|
title_blocks = magic_model.get_title_blocks(page_id)
|
105
|
-
inline_equations, interline_equations, interline_equation_blocks =
|
119
|
+
inline_equations, interline_equations, interline_equation_blocks = (
|
120
|
+
magic_model.get_equations(page_id)
|
121
|
+
)
|
106
122
|
|
107
123
|
page_w, page_h = magic_model.get_page_size(page_id)
|
108
124
|
|
109
125
|
spans = magic_model.get_all_spans(page_id)
|
110
126
|
|
111
|
-
|
112
|
-
if parse_mode ==
|
127
|
+
"""根据parse_mode,构造spans"""
|
128
|
+
if parse_mode == 'txt':
|
113
129
|
"""ocr 中文本类的 span 用 pymu spans 替换!"""
|
114
130
|
pymu_spans = txt_spans_extract(
|
115
131
|
pdf_docs[page_id], inline_equations, interline_equations
|
116
132
|
)
|
117
133
|
spans = replace_text_span(pymu_spans, spans)
|
118
|
-
elif parse_mode ==
|
134
|
+
elif parse_mode == 'ocr':
|
119
135
|
pass
|
120
136
|
else:
|
121
|
-
raise Exception(
|
137
|
+
raise Exception('parse_mode must be txt or ocr')
|
122
138
|
|
123
|
-
|
139
|
+
"""删除重叠spans中置信度较低的那些"""
|
124
140
|
spans, dropped_spans_by_confidence = remove_overlaps_low_confidence_spans(spans)
|
125
|
-
|
141
|
+
"""删除重叠spans中较小的那些"""
|
126
142
|
spans, dropped_spans_by_span_overlap = remove_overlaps_min_spans(spans)
|
127
|
-
|
128
|
-
spans = ocr_cut_image_and_table(
|
143
|
+
"""对image和table截图"""
|
144
|
+
spans = ocr_cut_image_and_table(
|
145
|
+
spans, pdf_docs[page_id], page_id, pdf_bytes_md5, imageWriter
|
146
|
+
)
|
129
147
|
|
130
|
-
|
148
|
+
"""将所有区块的bbox整理到一起"""
|
131
149
|
# interline_equation_blocks参数不够准,后面切换到interline_equations上
|
132
150
|
interline_equation_blocks = []
|
133
151
|
if len(interline_equation_blocks) > 0:
|
134
|
-
all_bboxes, all_discarded_blocks, drop_reasons =
|
135
|
-
|
136
|
-
|
152
|
+
all_bboxes, all_discarded_blocks, drop_reasons = (
|
153
|
+
ocr_prepare_bboxes_for_layout_split(
|
154
|
+
img_blocks,
|
155
|
+
table_blocks,
|
156
|
+
discarded_blocks,
|
157
|
+
text_blocks,
|
158
|
+
title_blocks,
|
159
|
+
interline_equation_blocks,
|
160
|
+
page_w,
|
161
|
+
page_h,
|
162
|
+
)
|
163
|
+
)
|
137
164
|
else:
|
138
|
-
all_bboxes, all_discarded_blocks, drop_reasons =
|
139
|
-
|
140
|
-
|
165
|
+
all_bboxes, all_discarded_blocks, drop_reasons = (
|
166
|
+
ocr_prepare_bboxes_for_layout_split(
|
167
|
+
img_blocks,
|
168
|
+
table_blocks,
|
169
|
+
discarded_blocks,
|
170
|
+
text_blocks,
|
171
|
+
title_blocks,
|
172
|
+
interline_equations,
|
173
|
+
page_w,
|
174
|
+
page_h,
|
175
|
+
)
|
176
|
+
)
|
141
177
|
|
142
178
|
if len(drop_reasons) > 0:
|
143
179
|
need_drop = True
|
144
180
|
drop_reason.append(DropReason.OVERLAP_BLOCKS_CAN_NOT_SEPARATION)
|
145
181
|
|
146
|
-
|
147
|
-
discarded_block_with_spans, spans = fill_spans_in_blocks(
|
182
|
+
"""先处理不需要排版的discarded_blocks"""
|
183
|
+
discarded_block_with_spans, spans = fill_spans_in_blocks(
|
184
|
+
all_discarded_blocks, spans, 0.4
|
185
|
+
)
|
148
186
|
fix_discarded_blocks = fix_discarded_block(discarded_block_with_spans)
|
149
187
|
|
150
|
-
|
188
|
+
"""如果当前页面没有bbox则跳过"""
|
151
189
|
if len(all_bboxes) == 0:
|
152
|
-
logger.warning(f
|
153
|
-
return ocr_construct_page_component_v2(
|
154
|
-
|
155
|
-
|
190
|
+
logger.warning(f'skip this page, not found useful bbox, page_id: {page_id}')
|
191
|
+
return ocr_construct_page_component_v2(
|
192
|
+
[],
|
193
|
+
[],
|
194
|
+
page_id,
|
195
|
+
page_w,
|
196
|
+
page_h,
|
197
|
+
[],
|
198
|
+
[],
|
199
|
+
[],
|
200
|
+
interline_equations,
|
201
|
+
fix_discarded_blocks,
|
202
|
+
need_drop,
|
203
|
+
drop_reason,
|
204
|
+
)
|
156
205
|
|
157
206
|
"""在切分之前,先检查一下bbox是否有左右重叠的情况,如果有,那么就认为这个pdf暂时没有能力处理好,这种左右重叠的情况大概率是由于pdf里的行间公式、表格没有被正确识别出来造成的 """
|
158
207
|
|
159
208
|
while True: # 循环检查左右重叠的情况,如果存在就删除掉较小的那个bbox,直到不存在左右重叠的情况
|
160
|
-
is_useful_block_horz_overlap, all_bboxes =
|
209
|
+
is_useful_block_horz_overlap, all_bboxes = (
|
210
|
+
remove_horizontal_overlap_block_which_smaller(all_bboxes)
|
211
|
+
)
|
161
212
|
if is_useful_block_horz_overlap:
|
162
213
|
need_drop = True
|
163
214
|
drop_reason.append(DropReason.USEFUL_BLOCK_HOR_OVERLAP)
|
164
215
|
else:
|
165
216
|
break
|
166
217
|
|
167
|
-
|
218
|
+
"""根据区块信息计算layout"""
|
168
219
|
page_boundry = [0, 0, page_w, page_h]
|
169
220
|
layout_bboxes, layout_tree = get_bboxes_layout(all_bboxes, page_boundry, page_id)
|
170
221
|
|
171
222
|
if len(text_blocks) > 0 and len(all_bboxes) > 0 and len(layout_bboxes) == 0:
|
172
223
|
logger.warning(
|
173
|
-
f
|
224
|
+
f'skip this page, page_id: {page_id}, reason: {DropReason.CAN_NOT_DETECT_PAGE_LAYOUT}'
|
225
|
+
)
|
174
226
|
need_drop = True
|
175
227
|
drop_reason.append(DropReason.CAN_NOT_DETECT_PAGE_LAYOUT)
|
176
228
|
|
177
229
|
"""以下去掉复杂的布局和超过2列的布局"""
|
178
|
-
if any(
|
230
|
+
if any(
|
231
|
+
[lay['layout_label'] == LAYOUT_UNPROC for lay in layout_bboxes]
|
232
|
+
): # 复杂的布局
|
179
233
|
logger.warning(
|
180
|
-
f
|
234
|
+
f'skip this page, page_id: {page_id}, reason: {DropReason.COMPLICATED_LAYOUT}'
|
235
|
+
)
|
181
236
|
need_drop = True
|
182
237
|
drop_reason.append(DropReason.COMPLICATED_LAYOUT)
|
183
238
|
|
184
239
|
layout_column_width = get_columns_cnt_of_layout(layout_tree)
|
185
240
|
if layout_column_width > 2: # 去掉超过2列的布局pdf
|
186
241
|
logger.warning(
|
187
|
-
f
|
242
|
+
f'skip this page, page_id: {page_id}, reason: {DropReason.TOO_MANY_LAYOUT_COLUMNS}'
|
243
|
+
)
|
188
244
|
need_drop = True
|
189
245
|
drop_reason.append(DropReason.TOO_MANY_LAYOUT_COLUMNS)
|
190
246
|
|
191
|
-
|
247
|
+
"""根据layout顺序,对当前页面所有需要留下的block进行排序"""
|
192
248
|
sorted_blocks = sort_blocks_by_layout(all_bboxes, layout_bboxes)
|
193
249
|
|
194
|
-
|
250
|
+
"""将span填入排好序的blocks中"""
|
195
251
|
block_with_spans, spans = fill_spans_in_blocks(sorted_blocks, spans, 0.3)
|
196
252
|
|
197
|
-
|
253
|
+
"""对block进行fix操作"""
|
198
254
|
fix_blocks = fix_block_spans(block_with_spans, img_blocks, table_blocks)
|
199
255
|
|
200
|
-
|
256
|
+
"""获取QA需要外置的list"""
|
201
257
|
images, tables, interline_equations = get_qa_need_list_v2(fix_blocks)
|
202
258
|
|
203
|
-
|
204
|
-
page_info = ocr_construct_page_component_v2(
|
205
|
-
|
206
|
-
|
259
|
+
"""构造pdf_info_dict"""
|
260
|
+
page_info = ocr_construct_page_component_v2(
|
261
|
+
fix_blocks,
|
262
|
+
layout_bboxes,
|
263
|
+
page_id,
|
264
|
+
page_w,
|
265
|
+
page_h,
|
266
|
+
layout_tree,
|
267
|
+
images,
|
268
|
+
tables,
|
269
|
+
interline_equations,
|
270
|
+
fix_discarded_blocks,
|
271
|
+
need_drop,
|
272
|
+
drop_reason,
|
273
|
+
)
|
207
274
|
return page_info
|
208
275
|
|
209
276
|
|
210
|
-
def pdf_parse_union(
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
277
|
+
def pdf_parse_union(
|
278
|
+
pdf_bytes,
|
279
|
+
model_list,
|
280
|
+
imageWriter,
|
281
|
+
parse_mode,
|
282
|
+
start_page_id=0,
|
283
|
+
end_page_id=None,
|
284
|
+
debug_mode=False,
|
285
|
+
):
|
218
286
|
pdf_bytes_md5 = compute_md5(pdf_bytes)
|
219
|
-
pdf_docs = fitz.open(
|
287
|
+
pdf_docs = fitz.open('pdf', pdf_bytes)
|
220
288
|
|
221
|
-
|
289
|
+
"""初始化空的pdf_info_dict"""
|
222
290
|
pdf_info_dict = {}
|
223
291
|
|
224
|
-
|
292
|
+
"""用model_list和docs对象初始化magic_model"""
|
225
293
|
magic_model = MagicModel(model_list, pdf_docs)
|
226
294
|
|
227
|
-
|
295
|
+
"""根据输入的起始范围解析pdf"""
|
228
296
|
# end_page_id = end_page_id if end_page_id else len(pdf_docs) - 1
|
229
|
-
end_page_id =
|
297
|
+
end_page_id = (
|
298
|
+
end_page_id
|
299
|
+
if end_page_id is not None and end_page_id >= 0
|
300
|
+
else len(pdf_docs) - 1
|
301
|
+
)
|
230
302
|
|
231
303
|
if end_page_id > len(pdf_docs) - 1:
|
232
|
-
logger.warning(
|
304
|
+
logger.warning('end_page_id is out of range, use pdf_docs length')
|
233
305
|
end_page_id = len(pdf_docs) - 1
|
234
306
|
|
235
|
-
|
307
|
+
"""初始化启动时间"""
|
236
308
|
start_time = time.time()
|
237
309
|
|
238
310
|
for page_id, page in enumerate(pdf_docs):
|
239
|
-
|
311
|
+
"""debug时输出每页解析的耗时."""
|
240
312
|
if debug_mode:
|
241
313
|
time_now = time.time()
|
242
314
|
logger.info(
|
243
|
-
f
|
315
|
+
f'page_id: {page_id}, last_page_cost_time: {get_delta_time(start_time)}'
|
244
316
|
)
|
245
317
|
start_time = time_now
|
246
318
|
|
247
|
-
|
319
|
+
"""解析pdf中的每一页"""
|
248
320
|
if start_page_id <= page_id <= end_page_id:
|
249
|
-
page_info = parse_page_core(
|
321
|
+
page_info = parse_page_core(
|
322
|
+
pdf_docs, magic_model, page_id, pdf_bytes_md5, imageWriter, parse_mode
|
323
|
+
)
|
250
324
|
else:
|
251
325
|
page_w = page.rect.width
|
252
326
|
page_h = page.rect.height
|
253
|
-
page_info = ocr_construct_page_component_v2(
|
254
|
-
|
255
|
-
|
256
|
-
pdf_info_dict[f
|
327
|
+
page_info = ocr_construct_page_component_v2(
|
328
|
+
[], [], page_id, page_w, page_h, [], [], [], [], [], True, 'skip page'
|
329
|
+
)
|
330
|
+
pdf_info_dict[f'page_{page_id}'] = page_info
|
257
331
|
|
258
332
|
"""分段"""
|
259
333
|
para_split(pdf_info_dict, debug_mode=debug_mode)
|
@@ -261,7 +335,7 @@ def pdf_parse_union(pdf_bytes,
|
|
261
335
|
"""dict转list"""
|
262
336
|
pdf_info_list = dict_to_list(pdf_info_dict)
|
263
337
|
new_pdf_info_dict = {
|
264
|
-
|
338
|
+
'pdf_info': pdf_info_list,
|
265
339
|
}
|
266
340
|
|
267
341
|
return new_pdf_info_dict
|