import xml.etree.ElementTree as ET
from bs4 import BeautifulSoup
import pandas as pd
def parse_kml(kml_file):
tree = ET.parse(kml_file)
root = tree.getroot()
data = []
for placemark in root.findall('.//{http://www.opengis.net/kml/2.2}Placemark'):
name = placemark.find('{http://www.opengis.net/kml/2.2}name').text
description = placemark.find('{http://www.opengis.net/kml/2.2}description').text
# Parse HTML content to extract text
if description:
soup = BeautifulSoup(description, 'html.parser')
description = soup.get_text()
coordinates = placemark.find('.//{http://www.opengis.net/kml/2.2}coordinates').text.strip().split(',')
coordinates = ','.join(coordinates[:2]) # Take only latitude and longitude
data.append([coordinates, name, description])
return data
def save_to_csv(data, csv_file):
df = pd.DataFrame(data, columns=['Coordinates', 'Area Name', 'Area Description'])
df.to_csv(csv_file, index=False)
# Example usage
kml_file = 'path/to/your/file.kml'
csv_file = 'path/to/output.csv'
data = parse_kml(kml_file)
save_to_csv(data, csv_file)