-
Notifications
You must be signed in to change notification settings - Fork 2
/
images2powerpoint
executable file
·81 lines (69 loc) · 2.36 KB
/
images2powerpoint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
#
# Author: Gokberk Cinbis
# February 2020
#
# Resources:
# * https://stackoverflow.com/questions/46184239/extract-a-page-from-a-pdf-as-a-jpeg
# * https://pythonprogramming.altervista.org/inserting-an-image-in-powerpoint-with-python
# * https://gist.github.com/glass5er/748cda36befe17fd1cb0
import os
import sys
from PIL import Image
def help_and_exit():
print('images2powerpoint <image1> ... <imageN> <output.pptx>')
print('Convert a series of images to powerpoint by taking image snapshots.')
print('Makes each slide covered by the corresponding image.')
print('For defining the aspect ratio of the presentation, uses the very first image.')
print('')
print('Note: to convert pdf to images, one can use: pdftoppm -r 300 input.pdf outdir/prefix -png')
print('')
print('INSTALL')
print('pip3 install python-pptx')
exit(1)
def parse_args(argv):
opts = {}
if len(argv) < 2:
help_and_exit()
opts['input'] = argv[1:(len(argv)-1)]
opts['output'] = argv[-1]
return opts
def get_img_size(path):
img = Image.open(path)
height = img.size[1]
width = img.size[0]
# print('imh: ' + str(height), 'imw: ' + str(width)) --> OK.
return height, width
def init_pptx(opts):
from pptx import Presentation
prs = Presentation()
# default slide width
#prs.slide_width = 9144000
# slide height @ 4:3
#prs.slide_height = 6858000
# slide height @ 16:9
prs.slide_height = 5143500 # English Metric Units (EMU)
imh, imw = get_img_size(opts['input'][0])
prs.slide_width = int(prs.slide_height / imh * imw)
return prs
# -- main code --
opts = parse_args(sys.argv)
prs = init_pptx(opts)
pic_left = int(0) # prs.slide_width * 0.15)
pic_top = int(0) # prs.slide_height * 0.1
pic_width = int(prs.slide_width) # * 0.7
for g in opts['input']:
slide = prs.slides.add_slide(prs.slide_layouts[6]) # blank layout
'''
example to add text
tb = slide.shapes.add_textbox(0, 0, prs.slide_width, pic_top / 2)
p = tb.textframe.add_paragraph()
p.text = g
p.font.size = pptx.util.Pt(14)
'''
imh, imw = get_img_size(g)
pic_height = int(pic_width * imh / imw)
#pic = slide.shapes.add_picture(g, pic_left, pic_top)
pic = slide.shapes.add_picture(g, pic_left, pic_top, pic_width, pic_height)
prs.save(opts['output'])
print('Done: ' + opts['output'])