-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.py
102 lines (78 loc) · 2.38 KB
/
util.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
import re
import fitz # PyMuPDF
import pdfplumber
def pdf_to_img(file_path):
# 打开PDF文件
pdf_document = fitz.open(file_path)
page = pdf_document[0]
rotate = int(0)
zoom_x = 1.33333333
zoom_y = 1.33333333
mat = fitz.Matrix(zoom_x, zoom_y)
mat = mat.prerotate(rotate)
pix = page.get_pixmap(matrix=mat, alpha=False)
temp_img_path = file_path + '.temp.png'
pix.save(temp_img_path)
pdf_document.close()
return temp_img_path
def find_first_num(text):
match = re.search(r'\d+(\.\d+)?', text)
if match:
first_number = match.group()
print(f"找到的第一个数字是:{first_number}")
return first_number
else:
print("没有找到数字。")
def find_second_num(text):
pattern = r'\d+(\.\d+)?'
match = re.search(pattern, text)
if match:
first_number = match.group()
print(f"找到的第一个数字是:{first_number}")
# 从第一个数字的末尾继续搜索下一个数字
start_index = match.end()
match = re.search(pattern, text[start_index:])
if match:
second_number = match.group()
print(f"找到的第二个数字是:{second_number}")
return second_number
else:
print("没有找到第二个数字。")
else:
print("没有找到数字。")
def pdf_read_text(path):
rs = []
with pdfplumber.open(path) as pdf:
page = pdf.pages[0]
lines = page.extract_words()
simple = page.extract_text_simple()
simple = simple.replace(' ', '')
print(simple)
simple = simple.split('\n')
for line in lines:
item = [
int(line.get('x0')),
int(line.get('top')),
int(line.get('x1')),
int(line.get('bottom')),
line.get('text')
]
print(item)
rs.append(item)
return rs,simple
def pdf_read_text_simple(path):
with pdfplumber.open(path) as pdf:
page = pdf.pages[0]
return rs
def is_number(text):
try:
float(text)
print("The text is a float.")
except ValueError:
print("The text is not a number.")
return False
return True
def find_numbers(text):
# 正则表达式匹配整数和小数
pattern = r'\d+\.\d+'
return re.findall(pattern, text)