MCPcopy Create free account
hub / github.com/917Dhj/DeepPaperNote / extract_pdf_sections

Function extract_pdf_sections

scripts/common.py:1901–1937  ·  view source on GitHub ↗
(pdf_path: Path, max_pages: int | None = None)

Source from the content-addressed store, hash-verified

1899
1900
1901def extract_pdf_sections(pdf_path: Path, max_pages: int | None = None) -> dict[str, str]:
1902 if fitz is None:
1903 return {}
1904 sections: dict[str, list[str]] = {"preamble": []}
1905 current = "preamble"
1906 doc = fitz.open(pdf_path)
1907 try:
1908 page_limit = len(doc) if max_pages is None else min(len(doc), max_pages)
1909 for page_index in range(page_limit):
1910 text = doc[page_index].get_text("text")
1911 reached_stop = False
1912 for raw_line in text.splitlines():
1913 line = clean_pdf_line(raw_line)
1914 if not line:
1915 continue
1916 heading = match_section_heading(line)
1917 if heading == "stop":
1918 reached_stop = True
1919 break
1920 if heading:
1921 current = heading
1922 sections.setdefault(current, [])
1923 continue
1924 sections.setdefault(current, []).append(line)
1925 if reached_stop:
1926 break
1927 finally:
1928 doc.close()
1929
1930 collapsed = {}
1931 for key, value in sections.items():
1932 if not value:
1933 continue
1934 text = re.sub(r"\s+", " ", " ".join(value)).strip()
1935 if text:
1936 collapsed[key] = text
1937 return collapsed
1938
1939
1940def extract_pdf_text(pdf_path: Path, max_pages: int | None = None) -> str:

Callers 2

mainFunction · 0.90

Calls 5

clean_pdf_lineFunction · 0.85
match_section_headingFunction · 0.85
openMethod · 0.45
get_textMethod · 0.45
closeMethod · 0.45