-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection_parser.py
More file actions
63 lines (47 loc) · 1.53 KB
/
section_parser.py
File metadata and controls
63 lines (47 loc) · 1.53 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
import fitz # PyMuPDF
import os
def is_bold(span):
if span.get("flags", 0) & 2:
return True
if "bold" in span.get("font", "").lower():
return True
return False
def is_likely_heading(text, size, span):
if not text:
return False
bold = is_bold(span)
if size < 12:
return False
if text.isupper():
return True
if text.istitle() and 1 <= len(text.split()) <= 10:
if bold or size >= 13:
return True
if bold and len(text.split()) <= 10:
return True
return False
def extract_sections(pdf_path):
if not os.path.exists(pdf_path):
raise FileNotFoundError(f"File not found: {pdf_path}")
doc = fitz.open(pdf_path)
sections = {}
current_section = "Introduction"
sections[current_section] = ""
for page in doc:
blocks = page.get_text("dict")["blocks"]
for block in blocks:
if "lines" not in block:
continue
for line in block["lines"]:
for span in line["spans"]:
text = span["text"].strip()
size = span["size"]
if is_likely_heading(text, size, span):
current_section = text
if current_section not in sections:
sections[current_section] = ""
else:
sections[current_section] += text + " "
for sec in sections:
sections[sec] = sections[sec].strip()
return sections