Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

Unzip and zip files in Python tool

knozawa
11 - Bolide

Hello,

 

Does anyone know how to unzip and zip files in python tool?  Here is the related Alteryx community question about updating a PowerPoint by modifying XML file.

 

1. unzip PowerPoint

How can we extract PowerPoint using a Python tool?

 

2. zip files (_rels, docProps, ppt, and [Content_Types].xml)

After extracting the PowerPoint and modifying some xml files, I would like to zip the files again.

image_1.png

from ayx import Alteryx
import zipfile

with zipfile.ZipFile('C:/Temp/PowerPoint test/DELETE_modified.zip', 'w', compression = zipfile.ZIP_DEFLATED) as my_zip:
    my_zip.write('C:/Temp/PowerPoint test/DELETE_original/_rels')
    my_zip.write('C:/Temp/PowerPoint test/DELETE_original/docProps')
    my_zip.write('C:/Temp/PowerPoint test/DELETE_original/ppt')
    my_zip.write('C:/Temp/PowerPoint test/DELETE_original/[Content_Types].xml')

 

Error: 

ValueError: ZIP does not support timestamps before 1980

 

I removed the last line (...[Content_Types].xml)  and ran the workflow to test.  It created zip file, but the folders were empty.

image_2.png

3 REPLIES 3
knozawa
11 - Bolide

The first part (unzip the PowerPoint), I could do it using following script:

from ayx import Alteryx
import zipfile

with zipfile.ZipFile('C:/Temp/PowerPoint test/DELETE_original.pptx', 'r') as my_zip:
    my_zip.extractall('C:/Temp/PowerPoint test/DELETE_modified')

 

kelly_gilbert
13 - Pulsar

This is a Python issue, rather than an Alteryx one, but I used this example to get my ppt file zipped up:
https://www.geeksforgeeks.org/working-zip-files-python/

(scroll down to section 2).

 

The issue with blank folders is that you need to write each individual file (rather than just the folder path) to the zip object. In the example I linked above, it uses a function to walk the directories to generate a list of all of the child directories and files. Then, it uses a loop to write each individual file to the zip object.

 

On the Value error, oddly your [Content_Types].xml file has a modified date of 12/31/1979... Try opening the file and resaving it, so the modified date will be >= 1980!

Jon-Knepper
8 - Asteroid

@kelly_gilbert 

 

Thank you for providing your input on this.  I have been trying to accomplish something similar to this, but I am running into issues with getting the zipped file written to a specific location following the example from the link you posted.  The code have have indicates its successful but I am unable to find the file.  I think I am close, but I don't know what I am missing to get it to the right place.

 

Would it be possible for you to provide your python code as an example?

 

Thank you for your help!

Labels