Struggling with import win32com.client and reading pptx

Hi. I am trying to write a code to dispatch PowerPoint using win32com.client. It runs fine when I run my stramlit app locally. But when I deployed the application it failed.
Here is an example:

import win32com.client
powerpoint = win32com.client.Dispatch(“Powerpoint.Application”)
deck = powerpoint.Presentations.Open(‘file.pptx’, WithWindow=False)

You cannot use this package on streamlit cloud, since this is a windows-only package.
You have to go for a different approach.

Thank you Franky for your fast response. Is there any approach that you would recommend? thanks again

I can’t say with so little information.
It depends mainly on what you want to achieve.
But Microsoft Office on Headless Linux Server is quite a hairy thing.

All that I need from this, is to take images from a PowerPoint file and save them in a dictionary for later use. If you come across any different approach, please dont hesitate to share. I have been struggling with this for some time now

Yes there is indeed a real good trick for your problem :sunglasses:
And you don’t need any external libraries. :nerd_face:
But it only works for pptx files, not for older ppt files!
Because pptx files are technically zip files under the hood, so you can just unzip them and pick out the image files. Here are some code snippets to give you an idea how that can be accomplished:

import zipfile
from pathlib import Path

List all image files in pptx file

# load pptx file
with zipfile.ZipFile('test.pptx') as ziparchive:
    # filter all image files (png, jpg, gif...) from zip archive in pptx
    images = [f for f in ziparchive.namelist() if f.endswith(('.png', '.jpg', '.gif', '.jpeg', '.bmp', '.tiff', '.tif', '.svg'))]
# show only list of image files:
print(images)

Extract all image files to subdir and keeping file structure

# load pptx file
with zipfile.ZipFile('test.pptx') as ziparchive:
    # filter all image files (png, jpg, gif...) from zip archive in pptx
    images = [f for f in ziparchive.namelist() if f.endswith(('.png', '.jpg', '.gif', '.jpeg', '.bmp', '.tiff', '.tif', '.svg'))]
    for img in images:
        print(img)
        # extract image files from zip archive, it keeps the file structure
        ziparchive.extract(member=img, path='test')

Extract all image files to subdir and flatten the file structure

# load pptx file
with zipfile.ZipFile('test.pptx') as ziparchive:
    # filter all image files (png, jpg, gif...) from zip archive in pptx
    images = [f for f in ziparchive.namelist() if f.endswith(('.png', '.jpg', '.gif', '.jpeg', '.bmp', '.tiff', '.tif', '.svg'))]
    subdir = Path('test')
    subdir.mkdir(exist_ok=True)  # create directory if not exists
    for img in images:
        # extract image files from zip archive, it flattens out the file structure:
        img_file_export_path = subdir.joinpath(Path(img).name)
        print(img_file_export_path)
        with open(img_file_export_path, 'wb') as f:
            f.write(ziparchive.read(img))

You have to adjust the examples of course to your needs…

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.