Is there a simple way to combine KML files into one KML file with Python?

Nothing complex, I just want to append them to each other.

Why I want it to be a script: Some people in our company create KML files of work site boundaries to display in Google Earth. The idea is to make these all accessible to everyone - my thought is that when a staff member creates a new KML, they can add it to a specific folder on the server. The script will run nightly to combine them, and all staff will have a network link in Google Earth to the folder - thus, they daily get new information in Google Earth.

This was a staff request, so I'm looking into it.

I installed the pyKML library with pip (latest version), but when I try to import it (in Atom with the Script package), I get the following error: ModuleNotFoundError: No module named 'pyKML'

Path appears to be correct.

Can anyone provide assistance with pyKML or an alternate solution?

  • Have you seen: stackoverflow.com/questions/36394056/… ? You mention the path appears correct but it sounds like a path error.Have you tried importing pyKML with another editor or shell? Apr 14, 2020 at 20:52
  • Yes, you probably have more than one Python installation and pip installed pyKML to a version that is different the version you are running the script with? Maybe you are running this script using an IDE that is pointing to a different version of Python. Check Python paths, environment settings at the command prompt for your OS and your IDE settings. This will very with OS and IDE. You mention this all happening on a server. I think you will need to have Python (and required modules) installed there as well or make executables from your script to run on the server.
    – GBG
    Apr 14, 2020 at 23:13

2 Answers 2


You can use the fastkml python lib for this. It can be found at https://fastkml.readthedocs.io/en/latest/ with all instructions to the usage.

Here is a code example for using this library to generate a kml structure.

from pathlib import Path

from fastkml import kml
from shapely.geometry import shape

def read_kml(kml_text):
    k = kml.KML()

    # Reading the kml from a text string

    k_doc = list(k.features())[0]

    # Getting the first document child that must be a folder or a placemark
    # but you can iterate over it to get multiple folders or placemarks
    doc_child = list(k_doc.features())[0]

    lst_geoms = list()
    if isinstance(doc_child, kml.Folder):
        for feature in doc_child.features():
    elif isinstance(doc_child, kml.Placemark):

    # Returning the geometries found list  
    return lst_geoms

def generate_kml():
    # Final file IO
    k = kml.KML()
    ns = '{http://www.opengis.net/kml/2.2}'

    # Creating document structure
    document = kml.Document(

    # Creating folder structure
    # Note that a kml can have multiple folders
    folder = kml.Folder(

    # Creating placemark structure
    # Note that a kml can have multiple placemarks
    placemark = kml.Placemark(
    # Passing the geometry to the placemark
    # It can be a shapely geom (Polygon, MultiPolygon, Polyline, Point, etc)
    placemark.geometry = shape("SOME_GEOJSON_GEOMETRY_HERE")

    # Adding the placemark to the previous created folder

    final_kml_text = k.to_string(prettyprint=True)

    output = Path(__file__).parent / "test.kml"

if __name__ == '__main__':
    # U can either generate a kml from a existing data
    # or read another kml to get the features and properties
    # to generate your own
    geometries = read_kml("SOME_KML_STRINGIFIED_TEXT")

  • having a little trouble understanding how to actually combine kml files using this script. Let's say I have a list containing 10 kml file paths I want to turn into one combined kml file - how do you use this code to accomplish that? I'll keep working on it to see if I can answer my own question too...
    – mTy
    Oct 14, 2022 at 23:48

I experienced this problem just now and came up on this answer. I noticed the error message had pyKML with capitalized letters. It will work if you use all lowercase letters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.