adierebel / Bearhug Packing List Generator

04 Feb 2018 at 08:57

1 Windows Registry Editor Version 5.00
2
3 [HKEY_CLASSES_ROOT\Directory\shell\Create Packing List\command]
4 @="C:\\App\\Python\\pythonw.exe C:\\App\\Packer\\packer.py %1"
1 import ctypes
2 import sys
3 import glob
4 import os
5 from openpyxl import Workbook
6 from openpyxl.drawing.image import Image
7 from random import randrange
8
9 def alert(title, text, style):
10 '''Styles:
11 0: OK
12 1: OK | Cancel
13 2: Abort | Retry | Ignore
14 3: Yes | No | Cancel
15 4: Yes | No
16 5: Retry | No
17 6: Cancel | Try Again | Continue
18 '''
19 return ctypes.windll.user32.MessageBoxW(0, text, title, style)
20
21 def get_selected_path():
22 Args = sys.argv
23 if len(Args) > 1:
24 return Args[1]
25 return None
26
27 def calculate_aspect_ratio(Object):
28 dWidth = 200
29 dHeight = 200
30 sourceWidth = Object.width
31 sourceHeight = Object.height
32
33 if sourceWidth > sourceHeight:
34 newHeight = dWidth * sourceHeight / sourceWidth;
35 newWidth = dWidth
36 else:
37 newWidth = dHeight * sourceWidth / sourceHeight;
38 newHeight = dHeight;
39
40 return (newWidth, newHeight)
41
42
43 def create_packing_list(Path):
44 filename = "%s/packing_list_%s.xlsx" % (Path, randrange(1000, 9999))
45 wb = Workbook()
46 ws = wb.active
47 ws.title = "Packing List"
48
49 # Search Image
50 Files = []
51 Extensions = ['*.jpg', '*.png']
52 for Ex in Extensions:
53 Files.extend(glob.glob(os.path.join(Path, Ex)))
54
55 # Add Content
56 Cell = 1
57 for File in Files:
58 CellImage = "A%s" % (Cell)
59 CellName = "B%s" % (Cell)
60 Img = Image(File)
61 newWidth, newHeight = calculate_aspect_ratio(Img)
62 Img.height = newHeight
63 Img.width = newWidth
64 ws.add_image(Img, CellImage)
65 Name = os.path.basename(File)
66 Name = os.path.splitext(Name)[0]
67 ws[CellName] = Name
68 rd = ws.row_dimensions[Cell] # get dimension of current row
69 rd.height = newHeight
70 Cell += 1
71
72 # Save
73 wb.save(filename)
74 alert('Done', "Packing List Created!", 0)
75
76 Path = get_selected_path()
77 if Path:
78 create_packing_list(Path)
79
1 openpyxl
2 pillow

Comments (0)