1

Is there a way to do the following directly in python, or do I need to use a subprocess call?

$ unzip -p /Users/david/Desktop/new2.xlsx xl/sharedStrings.xml > shared3.xml

Something better than:

s=subprocess.run('unzip -p /Users/david/Desktop/new2.xlsx xl/sharedStrings.xml', shell=True, stdout=subprocess.PIPE)
print(s)
David542
  • 104,438
  • 178
  • 489
  • 842
  • Potentially related: https://stackoverflow.com/questions/3451111/unzipping-files-in-python – A.J. Uppal May 09 '20 at 04:52
  • This is your exact use case. https://stackoverflow.com/questions/10568468/merge-multiple-zip-files-into-a-single-zip-file-in-python – Sean Payne May 09 '20 at 04:53
  • Does this answer your question? [Unzipping files in Python](https://stackoverflow.com/questions/3451111/unzipping-files-in-python) – Prayson W. Daniel May 09 '20 at 05:22

1 Answers1

0

You can use the zipfile module:

from pathlib import Path
from zipfile import ZipFile

with ZipFile("/Users/david/Desktop/new2.xlsx") as zip_file:
    with zip_file.open("xl/sharedStrings.xml") as file_in_zip:
        Path("shared3.xml").write_bytes(file_in_zip.read())
Andrew
  • 396
  • 1
  • 5