Files
Aislo/resources/tester/scratch/search_pdf_exact.py
T

46 lines
1.5 KiB
Python

import pymupdf
import sys
import re
from pathlib import Path
sys.stdout.reconfigure(encoding='utf-8')
pdf_path = Path("resources/knowledge/original/행정규칙/임도 품셈 적용기준 (현 산림사업 표준품셈)/첨부/(산림청고시 제2025-82호) 산림사업 표준품셈.pdf")
doc = pymupdf.open(pdf_path)
print(f"Loaded PDF with {len(doc)} pages.")
# Search pages for keywords
# 1. 천공기 휘발유 (F0046)
# 2. 2-2-5. 천공기 손료 (F0068)
# 3. 3-2. 작업로 선정 (F0076)
# 4. 3-3. 작업로 설치 (F0077)
keywords = [
("F0046", "천공기 (휘발유)"),
("F0068", "2-2-5. 천공기"),
("F0076", "3-2. 작업로 선정"),
("F0077", "3-3. 작업로 설치")
]
for label, kw in keywords:
print(f"\n==================== Searching for {label}: '{kw}' ====================")
found = False
for page_num in range(len(doc)):
page = doc[page_num]
text = page.get_text()
if kw in text:
found = True
print(f"Found on PDF Page {page_num + 1} (0-indexed: {page_num}):")
# Print surrounding text
lines = text.splitlines()
for idx, l in enumerate(lines):
if kw in l:
start_l = max(0, idx - 8)
end_l = min(len(lines), idx + 20)
for k in range(start_l, end_l):
print(f" [{k:3d}] {lines[k]}")
break
if not found:
print(f"NOT FOUND for {kw}")