final
This commit is contained in:
83
algam1.py
Normal file
83
algam1.py
Normal file
@@ -0,0 +1,83 @@
|
||||
import os
|
||||
import xml.etree.ElementTree as ET
|
||||
import pandas as pd
|
||||
from pathlib import Path
|
||||
|
||||
# === 🔧 НАСТРОЙКИ (меняйте под себя) ===
|
||||
FOLDER_WITH_XMLS = r"C:\Users\jze9\Documents\work\git\kadastr12\test" # Папка с XML-файлами
|
||||
OUTPUT_XLSX_FILE = r"C:\Users\jze9\Documents\work\git\kadastr12\result.xlsx" # Куда сохранить результат
|
||||
|
||||
# === ⛏ ВСПОМОГАТЕЛЬНЫЕ ФУНКЦИИ ===
|
||||
def extract_text(elem, tag):
|
||||
node = elem.find(f".//{tag}")
|
||||
return node.text.strip() if node is not None and node.text else ""
|
||||
|
||||
def has_spatial(elem):
|
||||
return elem.find(".//SpatialElement") is not None
|
||||
|
||||
def collect_explanations(elem):
|
||||
return " ".join(e.text.strip() for e in elem.findall(".//Explanation") if e.text and e.text.strip())
|
||||
|
||||
def process_section(tree, tag, label, records, is_zemel=True):
|
||||
section = tree.find(f".//{tag}")
|
||||
if section is None:
|
||||
return
|
||||
subtag = next(iter(section), None)
|
||||
if subtag is None:
|
||||
return
|
||||
for item in section.findall(f".//{subtag.tag}"):
|
||||
rec = {
|
||||
"Раздел КПТР": label,
|
||||
"Кадастровый номер": extract_text(item, "CadastralNumber"),
|
||||
"Кадастровый квартал": extract_text(item, "CadastralBlock"),
|
||||
"Вид объекта": extract_text(item, "ObjectType"),
|
||||
"Площадь ЗУ": extract_text(item.find("Area") or item, "Area") if is_zemel else "",
|
||||
"Погрешность определения площади": extract_text(item.find("Area") or item, "Inaccuracy") if is_zemel else "",
|
||||
"наличие координат": "да" if has_spatial(item) else "нет",
|
||||
"Пояснения": collect_explanations(item),
|
||||
}
|
||||
records.append(rec)
|
||||
|
||||
# === 🚀 ОСНОВНОЙ КОД ===
|
||||
def main():
|
||||
records = []
|
||||
for file in os.listdir(FOLDER_WITH_XMLS):
|
||||
if not file.lower().endswith(".xml"):
|
||||
continue
|
||||
xml_path = os.path.join(FOLDER_WITH_XMLS, file)
|
||||
try:
|
||||
tree = ET.parse(xml_path)
|
||||
except ET.ParseError as e:
|
||||
print(f"❌ Ошибка в файле {file}: {e}")
|
||||
continue
|
||||
|
||||
root = tree.getroot()
|
||||
process_section(root, "FormParcels", "Образование ЗУ", records)
|
||||
process_section(root, "SpecifyParcels", "Уточнение ЗУ", records)
|
||||
process_section(root, "CadastralErrorsParcels", "Исправление ЗУ", records)
|
||||
process_section(root, "ExistNewObjectsRealty", "Уточнение ОКС", records, is_zemel=False)
|
||||
process_section(root, "CadastralErrorsOKS", "Исправление ОКС", records, is_zemel=False)
|
||||
|
||||
if not records:
|
||||
print("⚠️ Нет данных для экспорта.")
|
||||
return
|
||||
|
||||
for i, rec in enumerate(records, 1):
|
||||
rec["№ п/п"] = i
|
||||
|
||||
df = pd.DataFrame(records, columns=[
|
||||
"№ п/п",
|
||||
"Раздел КПТР",
|
||||
"Кадастровый номер",
|
||||
"Кадастровый квартал",
|
||||
"Вид объекта",
|
||||
"Площадь ЗУ",
|
||||
"Погрешность определения площади",
|
||||
"наличие координат",
|
||||
"Пояснения",
|
||||
])
|
||||
df.to_excel(OUTPUT_XLSX_FILE, index=False)
|
||||
print(f"✔️ Файл успешно сохранён: {OUTPUT_XLSX_FILE}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user