I have a string created from lxml's etree module in appropriate format for KML. It outputs ok as a KML file and opens in Google Earth ok. Here is the code for storing it:

stream_str = io.BytesIO(kml_str)
with open(f'{fname}.kml', 'wb') as f:

Some of the KML files are growing to over 8 mb, so I am looking to store in zipped format.

Currently, I'm using the Python zipfile package.

with zipfile.ZipFile(f'{fname}.kmz', 'w', compression=zipfile.ZIP_STORED) as zf:
    zf.writestr(f'{fname}.kml', stream_str.getvalue())

ZIP_STORED is the default compression, which is uncompressed. This KMZ opens in Google Earth ok, but doesn't give any advantage in storage size.

I've tried using compression=ZIP_LZMA and =ZIP_BZIP2 (https://docs.python.org/3/library/zipfile.html#zipfile.ZIP_BZIP2). Both save ok, and can be opened with WinZip to show a usable KML file. However, Google Earth does not open them. Going to the file menu and clicking Open, and pointing to the KMZ file results in Google Earth continuing to run and not actually opening anything.

How can I create KMZ in Python?

  • 1
    It's curious that you didn't try using the standard zip compression algorithm, zipfile.ZIP_DEFLATED
    – Vince
    Mar 19, 2023 at 0:35
  • Well, that worked. No excuse other than a n00b that was intimidated by ensuring I used the appropriate compression scale. Mar 19, 2023 at 23:16

1 Answer 1


In addition to ZIP_STORED (no compression), ZIP_DEFLATED is a supported compression method.

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.