Skip to content

Instantly share code, notes, and snippets.

@ganesh-k13
Last active October 18, 2020 16:25
Show Gist options
  • Save ganesh-k13/14589519e6ab42961134da9f7ff2611b to your computer and use it in GitHub Desktop.
Save ganesh-k13/14589519e6ab42961134da9f7ff2611b to your computer and use it in GitHub Desktop.
Excel images to ppt

Install pptx

pip install python-pptx

Run:

usage: parse.py [-h] --xls XLS --pptx PPTX

Process some integers.

optional arguments:
  -h, --help   show this help message and exit
  --xls XLS    Path to sheet with images
  --pptx PPTX  Path to output pptx

Example: python3 .\parse.py --xls .\mock.xlsx --pptx tmp.pptx

import zipfile
import argparse
from pptx import Presentation
class Excel:
def __init__(self, filename):
self.filename = filename
def _get_image_paths(self): # Source: https://stackoverflow.com/a/19306015/5671364
EmbeddedFiles = zipfile.ZipFile(self.filename).namelist()
ImageFiles = [F for F in EmbeddedFiles if F.count('.jpg') or F.count('.jpeg') ] # @rishi change to png or whatever
return iter(ImageFiles)
def dump_images_to_folder(self, location): # Move to location if needed, not doing now
file_paths = list()
for image in self._get_image_paths():
# pass
zipfile.ZipFile(self.filename).extract(image)
file_paths.append(image)
return file_paths
class PowerPoint:
def __init__(self, filename):
self.filename = filename
self.prs = Presentation()
def create_ppt_from_images(self, image_paths):
for i, image in enumerate(image_paths):
slide = self.prs.slides.add_slide(self.prs.slide_layouts[8])
placeholder = slide.placeholders[1]
picture = placeholder.insert_picture(image)
self.prs.save(self.filename)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--xls', action='store', type=str, required=True, help="Path to sheet with images")
parser.add_argument('--pptx', action='store', type=str, required=True, help="Path to output pptx")
args = parser.parse_args()
e_parser = Excel(args.xls)
image_paths = e_parser.dump_images_to_folder('/tmp')
p_dumper = PowerPoint(args.pptx)
p_dumper.create_ppt_from_images(image_paths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment