-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_exporter.py
39 lines (28 loc) · 994 Bytes
/
json_exporter.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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 29 16:10:08 2018
@author: Travis
"""
# json_exporter.py
import json
import os
import re
from miner_text_generator import extract_text_by_page
def export_as_json(pdf_path, json_path):
filename = os.path.splitext(os.path.basename(pdf_path))[0]
data = {'Filename': filename}
data['Pages'] = []
counter = 1
for page in extract_text_by_page(pdf_path):
text = re.sub("\x0c",' ',page)
# 连字符深恶痛绝!
text = text.replace('fi','fi').replace('fl','fl').replace('ff','ff').replace('ffi','ffi').replace('ffl','ffl')
page = {'Page_number':counter ,'text': text.lower()}
data['Pages'].append(page)
counter += 1
with open(json_path, 'w') as fh:
json.dump(data, fh)
if __name__ == '__main__':
pdf_path = 'StatisticalModels.pdf'
json_path = 'StatisticalModels.json'
export_as_json(pdf_path, json_path)