Skip to content

Instantly share code, notes, and snippets.

@synap5e
Created July 27, 2013 11:09
Show Gist options
  • Save synap5e/6094578 to your computer and use it in GitHub Desktop.
Save synap5e/6094578 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys, os
from wand.image import Image
from pptx import Presentation
from pptx.util import Px
pdf = sys.argv[1]
landscape_crops = [
(0.153, 0.137, 0.463, 0.465),
(0.537, 0.137, 0.848, 0.465),
(0.153, 0.534, 0.463, 0.862),
(0.537, 0.534, 0.848, 0.862),
]
portrait_crops = [
(0.199, 0.155, 0.800, 0.473),
(0.199, 0.525, 0.800, 0.844)
]
images = list()
index = 0
for page in range(0, 99):
try:
img = Image(filename='%s[%d]' % (pdf, page), resolution=300)
except:
break
if (img.size[0] > img.size[1]):
print "Landscape mode"
crops = landscape_crops
else:
print "Portrait mode"
crops = portrait_crops
for crop in crops:
cimg = img.clone()
cimg.crop(int(crop[0]*img.size[0]), int(crop[1]*img.size[1]), int(crop[2]*img.size[0]), int(crop[3]*img.size[1]))
path = '/tmp/%s.slide-%d.png' % (pdf, index)
images.append(path)
cimg.save(filename=path)
index += 1
prs = Presentation()
blank_slidelayout = prs.slidelayouts[6]
for image in images:
slide = prs.slides.add_slide(blank_slidelayout)
slide.shapes.add_picture(image, Px(0), Px(0), Px(720), Px(532))
os.remove(image)
filename = '%s.pptx' % pdf
print "Saved to %s" % filename
prs.save(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment