Skip to content

Instantly share code, notes, and snippets.

@spokzers
Last active September 2, 2017 18:24
Show Gist options
  • Select an option

  • Save spokzers/e3c67596bb836bd951b3e7e819fab374 to your computer and use it in GitHub Desktop.

Select an option

Save spokzers/e3c67596bb836bd951b3e7e819fab374 to your computer and use it in GitHub Desktop.
zip archive to pdf using python3

The script takes a .zip archive as an input, extracts it in /tmp/, gets all the extracted images and puts them in a PDF file and then saves that PDF in the CWD

Installation

Download zip and then install img2pdf using

pip install img2pdf

Usage:

python image2pdf some_archive.zip

Note:

requires python3 to work

import os
import sys
import img2pdf
import zipfile
def makepdf(directory, target):
files = [directory + '/' + f for f in os.listdir(directory)]
files.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
#print(files)
with open(target + ".pdf", "wb") as f:
f.write(img2pdf.convert(files))
#print(sys.argv)
base_dir = os.getcwd()
file_name = ''.join([i for i in sys.argv[1].split("/")[-1].split(".")[:-1]])
#print(file_name)
with zipfile.ZipFile(base_dir + '/' + sys.argv[1], "r") as z:
z.extractall("/tmp/" + file_name)
#file_path = ''.join([i for i in sys.argv[1].split(".")[:-1]])
#print(file_path)
makepdf('/tmp/' + file_name, os.getcwd() + '/' + file_name)
print("done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment