tm-desensitize/tmkit/extractor.py

30 lines
1014 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""文字抽取:原生页按词取 bbox,扫描页留 OCR 接口。"""
from typing import Dict, List
import fitz # PyMuPDF
Word = Dict # {"text": str, "bbox": (x0, y0, x1, y1)}
def extract_page_words(page: "fitz.Page") -> List[Word]:
"""抽取单页所有词及 bbox 坐标(PDF 坐标系,原点左上,单位 pt)。"""
words = page.get_text("words") # [x0, y0, x1, y1, word, block, line, word_no]
return [
{"text": w[4], "bbox": (w[0], w[1], w[2], w[3])}
for w in words
]
def extract_pdf_words(pdf_bytes: bytes) -> List[List[Word]]:
"""抽取整个 PDF,外层按页。扫描页抛 NotImplementedError。"""
from .splitter import classify_page
result: List[List[Word]] = []
with fitz.open(stream=pdf_bytes, filetype="pdf") as doc:
for page in doc:
if classify_page(page) == "scanned":
raise NotImplementedError("OCR 待接入 PaddleOCR")
result.append(extract_page_words(page))
return result