Last active
November 27, 2017 18:25
-
-
Save datadavev/b21f944b18c0ed441a0d80d030b7b493 to your computer and use it in GitHub Desktop.
Show the sizes of objects in a pptx file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Python script to show the size of images (bytes) on each slide | |
Requires pptx installed: | |
pip install -U python-pptx | |
''' | |
import os | |
import locale | |
import pptx | |
import sys | |
import logging | |
def dictSum(d): | |
t = 0 | |
for k in list(d.keys()): | |
t += d[k] | |
return t | |
def slideImageSizes(slide): | |
images = {} #sha1 : size | |
tbytes = 0 | |
for item in slide.shapes: | |
if isinstance(item, pptx.shapes.picture.Picture): | |
sha = item.image.sha1 | |
tbytes += len(item.image.blob) | |
if sha not in images: | |
images[sha] = len(item.image.blob) | |
return tbytes, images | |
def doWork(fname): | |
statinfo = os.stat(fname) | |
presentation = pptx.Presentation(fname) | |
slides = presentation.slides | |
all_images = {} | |
i = 1 | |
total = 0 | |
utotal = 0 | |
print("# ") | |
print("# N = slide number") | |
print("# T1 = total bytes of images on slide") | |
print("# T2 = total bytes for slide not including images counted elsewhere") | |
print(("{0:>3} : {1:>12} {2:>12}".format("N", "T1", "T2"))) | |
for slide in slides: | |
uisize = 0 | |
tsize, images = slideImageSizes(slide) | |
usize = dictSum(images) | |
total += tsize | |
for k in list(images.keys()): | |
if k not in all_images: | |
uisize += images[k] | |
all_images[k] = images[k] | |
tssize = locale.format("%d", tsize, grouping=True) | |
tusize = locale.format("%d", uisize, grouping=True) | |
print(("{0:>3} : {1:>12} {2:>12}".format(i, tssize, tusize))) | |
i += 1 | |
total += tsize | |
t_total = locale.format("%d", total, grouping=True) | |
u_total = locale.format("%d", dictSum(all_images), grouping=True) | |
print(("Total = {0:>12} {1:>12}".format(t_total, u_total) )) | |
print(("File size (compressed) = " + locale.format("%d", statinfo.st_size, grouping=True))) | |
if __name__ == "__main__": | |
locale.setlocale(locale.LC_ALL, 'en_US') | |
logging.basicConfig(level=logging.DEBUG) | |
try: | |
fname = sys.argv[1] | |
except: | |
print(("Oops. File name needed\nTry: {0} file_name.pptx".format(sys.argv[0]))) | |
sys.exit(0) | |
doWork(fname) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment