import ctypes import sys import glob import os from openpyxl import Workbook from openpyxl.drawing.image import Image from random import randrange def alert(title, text, style): '''Styles: 0: OK 1: OK | Cancel 2: Abort | Retry | Ignore 3: Yes | No | Cancel 4: Yes | No 5: Retry | No 6: Cancel | Try Again | Continue ''' return ctypes.windll.user32.MessageBoxW(0, text, title, style) def get_selected_path(): Args = sys.argv if len(Args) > 1: return Args[1] return None def calculate_aspect_ratio(Object): dWidth = 200 dHeight = 200 sourceWidth = Object.width sourceHeight = Object.height if sourceWidth > sourceHeight: newHeight = dWidth * sourceHeight / sourceWidth; newWidth = dWidth else: newWidth = dHeight * sourceWidth / sourceHeight; newHeight = dHeight; return (newWidth, newHeight) def create_packing_list(Path): filename = "%s/packing_list_%s.xlsx" % (Path, randrange(1000, 9999)) wb = Workbook() ws = wb.active ws.title = "Packing List" # Search Image Files = [] Extensions = ['*.jpg', '*.png'] for Ex in Extensions: Files.extend(glob.glob(os.path.join(Path, Ex))) # Add Content Cell = 1 for File in Files: CellImage = "A%s" % (Cell) CellName = "B%s" % (Cell) Img = Image(File) newWidth, newHeight = calculate_aspect_ratio(Img) Img.height = newHeight Img.width = newWidth ws.add_image(Img, CellImage) Name = os.path.basename(File) Name = os.path.splitext(Name)[0] ws[CellName] = Name rd = ws.row_dimensions[Cell] # get dimension of current row rd.height = newHeight Cell += 1 # Save wb.save(filename) alert('Done', "Packing List Created!", 0) Path = get_selected_path() if Path: create_packing_list(Path)