Files
kadastr-17/services/data_loader.py
2026-03-10 20:21:31 +05:00

47 lines
1.5 KiB
Python
Raw 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.
"""Загрузка исходных данных: Таблица 1 (КИ), Таблица 2, индекс MIF-файлов."""
from pathlib import Path
from openpyxl import load_workbook
def load_ki(path: str) -> dict:
"""Таблица 1: {название_поля → значение}."""
wb = load_workbook(path, data_only=True)
ws = wb.active
ki = {}
for row in ws.iter_rows(values_only=True):
if row[0] is not None and row[1] is not None:
ki[str(row[0]).strip()] = str(row[1]).strip()
return ki
def ki_val(ki: dict, keyword: str) -> str:
"""Найти значение в словаре КИ по подстроке ключа (без учёта регистра)."""
for k, v in ki.items():
if keyword.lower() in k.lower():
return v
return ""
def load_table2(path: str) -> list:
"""Таблица 2: список строк (кортежи). Заголовок и пустые строки пропускаются."""
wb = load_workbook(path, data_only=True)
ws = wb.active
rows = []
for i, row in enumerate(ws.iter_rows(values_only=True)):
if i == 0:
continue
if all(v is None for v in row[:5]):
continue
rows.append(row)
return rows
def build_mif_index(mif_dir: str) -> dict:
"""Индекс MIF-файлов: stem → полный путь."""
index = {}
for p in Path(mif_dir).rglob("*.mif"):
index[p.stem] = str(p)
return index