Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MM-Lehmann authored Apr 30, 2020
1 parent cb5cb73 commit 16f0dda
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
87 changes: 87 additions & 0 deletions pptx2h5p.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import sys
import os
from win32com import client
import json
from zipfile import ZipFile
from copy import deepcopy
import uuid

VERSION = '1.0'


def ppt2png(f):
powerpoint = client.Dispatch('Powerpoint.Application')
powerpoint.Presentations.Open(f)
powerpoint.ActivePresentation.Export(f, 'PNG')
powerpoint.ActivePresentation.Close()
powerpoint.Quit()


def add_to_json(newfile, image_folder, images, title):
exclude_files = ['content/content.json', 'h5p.json']
if getattr(sys, 'frozen', False):
basedir = sys._MEIPASS
else:
basedir = os.path.dirname(os.path.abspath(__file__))
template = os.path.join(basedir, 'template.h5p')

with ZipFile(template, 'r') as zin:
with ZipFile(newfile, 'w') as zout:
# copy all other files
for item in zin.infolist():
if item.filename not in exclude_files:
zout.writestr(item, zin.read(item.filename))

# bilder dateinamen dem content.json zufügen
with zin.open('content/content.json') as fp:
content = json.load(fp)
print(f"adding images from {image_folder}: {images}")
for i, image in enumerate(images):
if i > 0 and len(content['presentation']['slides']) <= i:
content['presentation']['slides'].append(deepcopy(content['presentation']['slides'][0]))
content['presentation']['slides'][i]['elements'][0]['action']['params']['file']['path'] = \
'images/' + image
content['presentation']['slides'][i]['elements'][0]['action']['subContentId'] = str(uuid.uuid4())
zout.writestr('content/content.json', json.dumps(content))

# bilder dem zip zufügen
for image in images:
zout.write(os.path.join(image_folder, image), 'content/images/' + image)

# titel ändern
with zin.open('h5p.json', 'r') as h5p:
content = json.load(h5p)
content['title'] = title
zout.writestr('h5p.json', json.dumps(content))


if __name__ == '__main__':
try:
print("Powerpoint to h5p Converter.")
print(f"Version: {VERSION}")
print("Martin Lehmann, 2020")
print("Licence: BSD-2-Clause")
print("Source code: https://github.com/MM-Lehmann/pptx2h5p")
if len(sys.argv) != 2:
print("Usage : python pptx2h5p.py [file]")
sys.exit(-1)

filepath = os.path.abspath(sys.argv[1])
print(f"extracting images from {filepath}.")
folder = os.path.dirname(filepath)
filename = os.path.basename(filepath)
title = os.path.splitext(filename)[0]
if not os.path.exists(filepath):
print("No such file!")
sys.exit(-1)

ppt2png(filepath)
image_folder = os.path.join(folder, title)
images = os.listdir(image_folder)
images.sort()
print("building new .h5p file")
add_to_json(os.path.splitext(filepath)[0] + '.h5p', image_folder, images, title)
print("Converting successfully finished.")
except Exception as e:
print(e)
input("Press Enter to close this window.")
33 changes: 33 additions & 0 deletions pptx2h5p.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['pptx2h5p.py'],
pathex=['C:\\Users\\u552588\\PycharmProjects\\pptx2h5p'],
binaries=[],
datas=[('template.h5p','.')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='pptx2h5p',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=True )
Binary file added template.h5p
Binary file not shown.

0 comments on commit 16f0dda

Please sign in to comment.