| from pathlib import Path |
|
|
| import httpx |
| import pymupdf |
|
|
| |
| DATA_DIR = Path.cwd() / "data" |
|
|
| |
| RAW_PDF_URLS = [ |
| "https://www.ebooksgratuits.com/ebooksfrance/moliere-oeuvres_completes_1.pdf", |
| "https://www.ebooksgratuits.com/ebooksfrance/moliere-oeuvres_completes_2.pdf", |
| ] |
|
|
|
|
| def download_pdf(url: str, output_path: str) -> None: |
| """Download a PDF from the given URL and save it to the specified output path.""" |
| with httpx.stream("GET", url, follow_redirects=True) as response: |
| response.raise_for_status() |
| with open(output_path, "wb") as f: |
| for chunk in response.iter_bytes(): |
| f.write(chunk) |
|
|
|
|
| def detect_toc(doc: pymupdf.Document) -> tuple[int, int]: |
| """Detect the table of contents in the first quarter of the document. |
| |
| Args: |
| doc: A pymupdf Document object representing the PDF document. |
| |
| Returns: |
| A tuple containing the start and end page indices of the TOC. |
| If no TOC is found, returns (-1, -1). |
| """ |
| start = -1 |
| end = -1 |
| i = 0 |
|
|
| for page in doc[: len(doc) // 4]: |
| if page.get_links(): |
| start = i |
| break |
| i += 1 |
|
|
| if start == -1: |
| print("No table of contents found in the first quarter of the document.") |
| return -1, -1 |
|
|
| for page in doc[start : len(doc) // 4]: |
| if not page.get_links(): |
| end = i |
| break |
| i += 1 |
|
|
| if end == -1: |
| end = len(doc) // 4 |
|
|
| print(f"Table of contents detected from page {start} to {end}.") |
| return start, end |
|
|
|
|
| def main(): |
| |
| DATA_DIR.mkdir(parents=True, exist_ok=True) |
|
|
| |
| paths = [] |
| for url in RAW_PDF_URLS: |
| filename = url.split("/")[-1] |
| output_path = DATA_DIR / filename |
| paths.append(output_path) |
| if not output_path.exists(): |
| print(f"Downloading {filename}...") |
| download_pdf(url, output_path) |
| else: |
| print(f"{filename} already exists, skipping download.") |
|
|
| |
| if (DATA_DIR / "tinymoliere.txt").exists(): |
| (DATA_DIR / "tinymoliere.txt").unlink() |
|
|
| |
| for path in paths: |
| with open(path, "rb") as f: |
| doc = pymupdf.open(f) |
|
|
| |
| _, toc_end = detect_toc(doc) |
|
|
| |
| rect = doc[0].bound() |
| new_rect = pymupdf.Rect( |
| rect.x0, rect.y0 + 60, rect.x1, rect.y1 - 60 |
| ) |
|
|
| |
| for page in doc[toc_end:-1]: |
| page_text = page.get_text(clip=new_rect) |
| with open(DATA_DIR / "tinymoliere.txt", "a") as f: |
| f.write(page_text) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|