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

62 lines
2.2 KiB
Python

import sys, json, re
import fitz # PyMuPDF
sys.stdout.reconfigure(encoding='utf-8')
pdf_path = "resources/knowledge/original/원가계산/건설공사_표준품셈/2026년_건설공사_표준품셈.pdf"
doc = fitz.open(pdf_path)
print(f"Total PDF pages: {len(doc)}")
# Load linked const work items
with open('resources/data_work_item_link/work_item_link_2026-01-01.json', 'r', encoding='utf-8') as f:
link_data = json.load(f)
with open('resources/data_work_item_master/const_work_item_master_2026-01-01.json', 'r', encoding='utf-8') as f:
const_master = json.load(f)
items = const_master.get('work_items', [])
item_map = {it['work_item_key']: it for it in items}
linked_keys = set()
for l in link_data.get('links', []):
ck = l.get('const_key')
if ck:
linked_keys.add(ck)
target_items = []
for k in sorted(linked_keys):
it = item_map.get(k)
if it:
target_items.append({
"key": k,
"number": it.get('number', ''),
"name": it.get('name', ''),
"path_name": it.get('path_name', ''),
"tables": it.get('tables', [])
})
print(f"Target linked items: {len(target_items)}")
# Search for section numbers in PDF
# Notice in const pumsem, section numbers look like "1-2-2", "3-3-6", etc.
for target in target_items:
sec_num = target['number']
sec_name = target['name']
found_pages = []
# regex pattern for section header: e.g. "1-2-2" followed by name or at line start
pattern = re.compile(rf"{re.escape(sec_num)}\s+.*{re.escape(sec_name[:4])}")
for page_idx in range(len(doc)):
text = doc[page_idx].get_text()
if re.search(pattern, text):
found_pages.append(page_idx + 1)
elif sec_num in text and sec_name[:3] in text:
if (page_idx + 1) not in found_pages:
found_pages.append(page_idx + 1)
target['pdf_pages'] = found_pages[:5] # keep up to first 5 matches
print(f"{target['key']} | {sec_num} | {sec_name} | pages={target['pdf_pages']}")
with open('scratch/located_const_sections.json', 'w', encoding='utf-8') as f:
json.dump(target_items, f, ensure_ascii=False, indent=2)