"""Find AC-X tag locations on floor plan pages using pdfplumber word extraction.""" import pdfplumber, re, sys, json from pathlib import Path pdf_path = Path(r'..\raw_pdfs\355 E 86th St Mechanical 100% CD Set_flattened.pdf') page_num = int(sys.argv[1]) if len(sys.argv) > 1 else 7 # 1-based with pdfplumber.open(pdf_path) as pdf: page = pdf.pages[page_num - 1] pw = page.width ph = page.height print(f'Page {page_num}: {pw:.0f} x {ph:.0f} pts') words = page.extract_words(x_tolerance=5, y_tolerance=5) # Find "AC" followed by a single letter [A-U] within close x-proximity hits = [] for i, w in enumerate(words): text = w['text'].strip() # Direct match: "ACD", "AC-D", "AC D" within one word m = re.match(r'^AC[-\s]?([A-U])$', text, re.IGNORECASE) if m: tag = f"AC-{m.group(1).upper()}" hits.append({'tag': tag, 'x0': w['x0'], 'y0': w['top'], 'x1': w['x1'], 'y1': w['bottom'], 'text': text}) continue # Two-word match: word "AC" followed immediately by a single letter if text.upper() == 'AC' and i + 1 < len(words): nxt = words[i + 1] if re.match(r'^[A-U]$', nxt['text'].strip(), re.IGNORECASE): # Check they're on same line and close horizontally if abs(nxt['top'] - w['top']) < 10 and (nxt['x0'] - w['x1']) < 30: tag = f"AC-{nxt['text'].strip().upper()}" hits.append({'tag': tag, 'x0': w['x0'], 'y0': w['top'], 'x1': nxt['x1'], 'y1': nxt['bottom'], 'text': f"AC {nxt['text']}"}) print(f'Found {len(hits)} AC-tag locations:') by_tag = {} for h in hits: by_tag.setdefault(h['tag'], []).append(h) for tag in sorted(by_tag): locs = by_tag[tag] print(f' {tag}: {len(locs)} occurrence(s)') for loc in locs[:5]: print(f' ({loc["x0"]:.0f},{loc["y0"]:.0f}) text="{loc["text"]}"') print(f'\nPage dimensions: {pw} x {ph} pts') print(json.dumps(hits[:20], indent=2))