Warning: Can not open [/home/conf/public_html/cgi-bin/show_python.log]. Ignore No title

Download script from pptx2img.py
Related files:


import os
import sys
import glob
from comtypes import client
from PIL import Image
import jaconv


infile = '41-01-search_MP.pptx'
out_dir = 'images'
image_format = 'png'
img_w = 480


nargv = len(sys.argv)
if nargv >= 2: infile = sys.argv[1]
if nargv >= 3: out_dir = sys.argv[2]
if nargv >= 4: image_format = sys.argv[3]


def export_img(fname, out_dir, image_format):
    application = client.CreateObject("PowerPoint.Application")
    application.Visible = True
    cur_dir = os.getcwd()

    print(f"Open [{fname}]")
    presentation = application.Presentations.Open(os.path.join(cur_dir, fname))

    export_path = os.path.join(cur_dir, out_dir)
    print(f"Export as images [png] to [{export_path}]")
    presentation.Export(export_path, FilterName = image_format)

    presentation.Close()
    application.Quit()

def rename_img(out_dir):
#    for i, fname in enumerate(sorted(file_list)):
    for i in range(1, 1000):
        new_fname = os.path.join(out_dir, f'{i:02d}.png')
        thumb_fname = os.path.join(out_dir, f'{i:02d}-small.png')

        print(f"Rename [{fname}] to [{new_fname}]")
        os.rename(fname, new_fname)

        print(f"Convert [{new_fname}] to thumbnail [{thumb_fname}]")
        img = Image.open(new_fname)
        height = int(img.height * img_w / img.width)
        img_resized = img.resize((img_w, height))
        try:
            img_resized.save(thumb_fname, quality = 90)
        except:
            print(f"Error: Can not convert {new_fname} to {thumb_fname}")


if __name__ == '__main__':
    export_img(infile, out_dir, image_format)
    rename_img(out_dir)