From 08365af2d4053ca747360d5c8646fe38469a744d Mon Sep 17 00:00:00 2001 From: Dan Dembinski Date: Thu, 6 May 2021 12:57:53 -0400 Subject: [PATCH] Added script and bat file to handle drag and dropped PDFs. Creates the thumbnails and moves the PDF. --- Thumbnails.py | 34 ++++++++++++++++++++++++++++++++++ thumbnail.bat | 2 ++ 2 files changed, 36 insertions(+) create mode 100644 Thumbnails.py create mode 100644 thumbnail.bat diff --git a/Thumbnails.py b/Thumbnails.py new file mode 100644 index 0000000..bc68ea3 --- /dev/null +++ b/Thumbnails.py @@ -0,0 +1,34 @@ +import pdf2image, shutil, sys, os + +file = sys.argv[1] +outfile = os.path.basename(file) +outputPath = 'C:\\Users\\ddembinski\\Desktop\\FOMO\\' + +# Gets PDF info, splits the 'Page size' key by the space and sets the width and height values +try: + info = pdf2image.pdfinfo_from_path(file, poppler_path='c:\\poppler\\bin') + # print(info['Page size'].split(' ')) + width = info['Page size'].split(' ')[0] + height = info['Page size'].split(' ')[2] + + + # Landscape + if float(width) > float(height): + images = pdf2image.convert_from_path(file, poppler_path='c:\\poppler\\bin', + output_folder=outputPath + '512', single_file=True, + size=(512,None), fmt='jpeg', output_file=outfile) + images = pdf2image.convert_from_path(file, poppler_path='c:\\poppler\\bin', + output_folder=outputPath + '128', single_file=True, + size=(128, None), fmt='jpeg', output_file=outfile) + # Portrait or square + else: + images = pdf2image.convert_from_path(file, poppler_path='c:\\poppler\\bin', + output_folder=outputPath + '512',single_file=True, + size=(None,512), fmt='jpeg', output_file=outfile) + images = pdf2image.convert_from_path(file, poppler_path='c:\\poppler\\bin', + output_folder=outputPath + '128', single_file=True, + size=(None, 128), fmt='jpeg', output_file=outfile) + shutil.move(file, outputPath + 'PDF\\' + str(outfile)) +except Exception as e: + print('%s - conversion failed' % (file)) + print(e) diff --git a/thumbnail.bat b/thumbnail.bat new file mode 100644 index 0000000..90dd762 --- /dev/null +++ b/thumbnail.bat @@ -0,0 +1,2 @@ +@echo off +cmd /k "cd /d C:\Users\ddembinski\PycharmProjects\fomoPDFs\Scripts & activate.bat & cd /d C:\Users\ddembinski\PycharmProjects\fomoPDFs & python Thumbnails.py %1" \ No newline at end of file