25 lines
792 B
Python
25 lines
792 B
Python
"""PDF 分流:检测每页有无文字层。"""
|
||
|
||
from typing import List
|
||
|
||
import fitz # PyMuPDF
|
||
|
||
|
||
def classify_page(page: "fitz.Page", min_chars: int = 20) -> str:
|
||
"""判断单页类型。
|
||
|
||
有文字层(get_text 非空白字符数 >= min_chars)→ 'native',
|
||
否则视为扫描页 → 'scanned'。min_chars 可调,避免只有页码的扫描页误判。
|
||
"""
|
||
text = page.get_text().strip()
|
||
return "native" if len(text) >= min_chars else "scanned"
|
||
|
||
|
||
def classify_pages(pdf_bytes: bytes, min_chars: int = 20) -> List[str]:
|
||
"""返回 PDF 每页类型列表 ['native'|'scanned', ...]。"""
|
||
out = []
|
||
with fitz.open(stream=pdf_bytes, filetype="pdf") as doc:
|
||
for page in doc:
|
||
out.append(classify_page(page, min_chars))
|
||
return out
|