#!/usr/bin/python # Name: extractdir.py # Version: 0.2 # Created: 2018-02-10 # Last modified: 2018-02-10 # Purpose: Extract any files embedded in an Excel spreadsheet, e.g., # Microsoft Visio or PowerPoint files, to a directory named "embedded" beneath # the current working directory. The Excel spreadsheet file name can be # provided on the command line; if it isn't the script will prompt for the # file name. import os, sys, zipfile dirToExtract = "xl/embeddings/" destinationDir = "embedded" # Check to see if the file name was entered on the command line. # If it wasn't prompt for the file name try: sys.argv[1] except IndexError: infile = raw_input("Zip file: ") else: infile = sys.argv[1] if not os.path.exists(destinationDir): os.makedirs(destinationDir) with zipfile.ZipFile(infile) as zip: for zip_info in zip.infolist(): if zip_info.filename.startswith(dirToExtract): zip_info.filename = os.path.basename(zip_info.filename) zip.extract(zip_info, destinationDir)