Problem compressing files in python with zipfile

33 Views Asked by At

I am trying to compress some files in python making use of zipfile, using the following code:

lista = ["_rels", "docProps", "word", "[Content_Types].xml"]
with zipfile.ZipFile("archivo.docx", "w", zipfile.ZIP_DEFLATED) as zip_file:
    for archivo in lista:
        zip_file.write(archivo)

But I am having the inconvenience that the folders: _rels, docProps and word, are compressed empty inside the generated zipfile. The only one that remains intact is the [Content_Types].xml file. Does anyone know why this is happening?

I have tried changing the compression format by adding zipfile.ZIP_DEFLATED, but still the problem remains.

Thanks in advance.

I clarify two things:

First, I don't just want to compress one folder, I need to compress 3 folders (with their files inside) and the external file, whose names have been mentioned before.

Second, it does not work for me if the files inside the folders are compressed and then saved in the zip file inside their corresponding folder. I am generating a .docx file and doing this damages the formatting.

Suggestions do not answer my question!!!!

0

There are 0 best solutions below