File size: 1,781 Bytes
0ebf67e | 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 | from __future__ import annotations
"""
Extract text from every page of a fragrance literature PDF using PyMuPDF.
Outputs a JSONL file where each line is:
{"source": "filename", "page": 1, "text": "..."}
"""
import argparse
import json
import logging
from pathlib import Path
import fitz
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger("extract_pdf_text")
def extract_text_from_pdf(pdf_path: Path) -> list[dict[str, str]]:
doc = fitz.open(str(pdf_path))
records = []
for i in range(len(doc)):
page = doc.load_page(i)
text = page.get_text()
records.append({
"source": pdf_path.stem,
"page": i + 1,
"text": text,
})
doc.close()
return records
def main() -> int:
parser = argparse.ArgumentParser(description="Extract text from PDF pages")
parser.add_argument("--input", required=True, help="PDF file or directory of PDFs")
parser.add_argument("--output", required=True, help="Output JSONL path")
args = parser.parse_args()
input_path = Path(args.input)
output_path = Path(args.output)
output_path.parent.mkdir(parents=True, exist_ok=True)
if input_path.is_dir():
pdf_files = sorted(input_path.glob("*.pdf"))
else:
pdf_files = [input_path]
with output_path.open("w", encoding="utf-8") as out:
for pdf_file in pdf_files:
logger.info("Extracting %s", pdf_file.name)
records = extract_text_from_pdf(pdf_file)
for record in records:
out.write(json.dumps(record, ensure_ascii=False) + "\n")
logger.info("Done: %s", output_path)
return 0
if __name__ == "__main__":
raise SystemExit(main())
|