-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmark_functions.py
More file actions
197 lines (185 loc) · 7.09 KB
/
Copy pathmark_functions.py
File metadata and controls
197 lines (185 loc) · 7.09 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
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
from reportlab.pdfgen import canvas
from PyPDF2 import PdfReader, PdfWriter
import io
def mapping_mark_line(soup,toExclude,mark_points,color):
for _ln in toExclude:
_line = soup.find('line',number=_ln)
pg = int(_line.get('page'))
xMin = float(_line.get('xmin'))
yMin = float(_line.get('ymin'))
xMax = float(_line.get('xmax'))
yMax = float(_line.get('ymax'))
width = xMax - xMin
height = yMax - yMin
element = _line
while element.name != 'page':
element = element.parent
page_height = float(element.get('height'))
coord = (((xMin, yMin), width, height), page_height,)
if pg not in mark_points:
mark_points[pg] = [
{
'number':_ln,
'coord': coord,
'color': color
}
]
else:
mark_points[pg].append(
{
'number':_ln,
'coord': coord,
'color': color
}
)
return mark_points
def mapping_mark_coord(soup,coords,mark_points,color):
for _pg,coords_page in coords.items():
for _coord in coords_page:
((x, y), width, height), page_height = _coord
if _pg not in mark_points:
mark_points[_pg] = [
{
'number':None,
'coord': _coord,
'color': color
}
]
else:
mark_points[_pg].append(
{
'number':None,
'coord': _coord,
'color': color
}
)
return mark_points
def _mark_bbox(pdf, boxers, output_pdf, pages):
"""
Marks bounding boxes on the specified pages of a PDF document.
Args:
pdf (str): The path to the input PDF file.
boxers (list): A list of bounding boxes to mark on the pages. Each bounding box is represented as a tuple
containing the coordinates ((x, y), width, height) and the page height.
output_pdf (str): The path to the output PDF file.
pages (tuple): A tuple specifying the range of pages to mark. If None, all pages will be marked.
color (tuple): The RGB color values (between 0 and 1) to use for marking the bounding boxes. Default is red (1, 0, 0).
"""
reader = PdfReader(pdf)
writer = PdfWriter()
if pages is not None:
pages_list = reader.pages[pages[0] - 1 : pages[1]]
else:
pages_list = reader.pages
_c = 0
for i, page in enumerate(pages_list):
if len(boxers) == 0 or not (_c in boxers):
writer.add_page(page)
_c += 1
continue
if _c >= len(boxers):
break
box = boxers[_c]
_c += 1
packet = io.BytesIO()
can = canvas.Canvas(packet)
for _box in box:
coord = _box['coord']
color = _box['color']
((x, y), width, height), page_height = coord
y = page_height - y - height
# Set color of the bbox
can.setStrokeColorRGB(*color)
can.setLineWidth(2)
can.rect(x, y, width, height)
can.showPage()
can.save()
packet.seek(0)
new_pdf = PdfReader(packet)
page.merge_page(new_pdf.pages[0])
writer.add_page(page)
with open(output_pdf, "wb") as f:
writer.write(f)
def find_coords(pdf_html, toExclude):
"""
Find the coordinates of specific lines in a PDF HTML.
Args:
pdf_html (BeautifulSoup): The parsed HTML of the PDF.
toExclude (list): A list of line numbers to exclude.
Returns:
list: A nested list of coordinates for each excluded line in each page.
Each coordinate is represented as a tuple of the form (((xMin, yMin), width, height),page_height).
The page height is also included in the tuple.
"""
if len(toExclude) == 0:
return []
coords = []
for _page in pdf_html.find_all('page'):
coords_page = []
for _line in _page.find_all('line'):
if int(_line.get('number')) in toExclude:
xMin = float(_line.get('xmin'))
yMin = float(_line.get('ymin'))
xMax = float(_line.get('xmax'))
yMax = float(_line.get('ymax'))
width = xMax - xMin
height = yMax - yMin
page_height = float(_page.get('height'))
coords_page.append((((xMin, yMin), width, height), page_height,))
coords.append(coords_page)
return coords
def coords_to_line(file_html, coords_tables):
numbers = []
for _p,_page in enumerate(file_html.find_all('page')):
tables = coords_tables[_p]
for _t,table in enumerate(tables):
((x,y),w,h),_ = table
for _l,_line in enumerate(_page.find_all('line')):
number = int(_line.get('number'))
xMin = round(float(_line.get('xmin')),2)
yMin = round(float(_line.get('ymin')),2)
xMax = round(float(_line.get('xmax')),2)
yMax = round(float(_line.get('ymax')),2)
width = xMax - xMin
height = yMax - yMin
page_height = float(_page.get('height'))
if xMin>=round(x,2) and yMin>=round(y,2) and round(x+w,2)>=xMax and round(y+h,2)>=yMax:
numbers.append(number)
return numbers
def coords_to_section(file_html, coords_lines_section):
coords = []
id_sections = []
coords_anexo = []
id_sections_anexo = []
for _t,(coords_section,_type) in enumerate(coords_lines_section):
number_l = []
coords_page = []
((x,y),w,h),pg_height,page,block_section = coords_section
# x,y = round(x,3),round(y,3)
pages = list(file_html.find_all('page'))
page_to_search = pages[page]
for _l,_line in enumerate(page_to_search.find_all('line')):
number = int(_line.get('number'))
xMin = float(_line.get('xmin'))
yMin = float(_line.get('ymin'))
xMax = float(_line.get('xmax'))
yMax = float(_line.get('ymax'))
width = xMax - xMin
height = yMax - yMin
page_height = pg_height
# if number == 85:
# # print(f"{_line}")
# print(xMin,yMin,xMax,yMax)
# print(x,y,w,h)
# print(xMin>=x,yMin>=y,w>=xMax,h>=yMax)
if xMin>=x and yMin>=y and w>=xMax and h>=yMax:
coords_page.append((((xMin,yMin),width,height),page_height))
block_section.append(_line)
number_l.append(number)
if _type == 'secao':
coords.append(coords_page)
id_sections.append(number_l)
elif _type == 'anexo':
coords_anexo.append(coords_page)
id_sections_anexo.append(number_l)
return (coords,id_sections),(coords_anexo,id_sections_anexo)