Python - compress folium map html as email attachment and send via sendgrid

35 Views Asked by At

I am trying to zip folium map html to reduce size in order to attach it to email sent out via Sendgrid.

map_content = m.get_root().render()
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zipf:
        zipf.writestr("event_map.html", map_content.encode('utf-8'))
        zip_buffer.seek(0)

attachments = [location_attachment, coverage_attachment, zip_buffer.getvalue()]
filenames = [
            "Raw Data 1.csv",
            "Data 2.csv",
            "event_map.zip"
        ]
filetypes = ["text/csv", "text/csv", "application/zip"]

This code allows me to create zip and send via email but the zip attachment cannot be opened and Windows throw Invalid Zip error.

Can anyone suggest a fix to it?

thank you

0

There are 0 best solutions below