from bs4 import BeautifulSoup import glob import os.path def parseTCX(filename): file = open(filename) xml_file = file.read() soup = BeautifulSoup(xml_file, 'lxml') id = soup.find("id").text lats = [] longs = [] for tag in soup.find_all("trackpoint"): lats.append(tag.find("latitudedegrees").text) longs.append(tag.find("longitudedegrees").text) return id[:-10], lats, longs def single_run(id, lats, longs): locString = "" for i in range(0, len(lats)): locString += longs[i] + "," + lats[i] + " " value = """ {0} xx Miles 1 clampToGround {1} """ return value.format(id, locString) def convertToKML(): base_path = os.path.dirname(os.path.realpath(__file__)) files = glob.glob(base_path + "/tcx/*.tcx") header = """ 1 1 Tracks 1 0 """ footer = """ """ o_file = open("outputKML.kml", "w") o_file.write(header) for file in files: id, lats, longs = parseTCX(file) o_file.write(single_run(id, lats, longs)) print(files) o_file.write(footer) o_file.close() convertToKML()