0

Hello stackoverflow users.

I am trying to download an image from the powerpoint presentation and then to process it(to recognize numbers on it at certain coordinates).

My problem is that I can download an image from pptx data only in .wmf format, and I cannot convert it. I have tried all possible solutions already.

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE

pptx_path = "name_pptx.pptx"

prs = Presentation(pptx_path)

desired_slide = prs.slides[6 - 1]

for shape in desired_slide.shapes:
    if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
        image_file_bytes = shape.image.blob
        file_extension = shape.image.ext # at this point format is .wfm

Interesting that in Powerpoint I can select a desired .jpeg extension when saving a file.

Andrey Mazur
  • 510
  • 4
  • 14

1 Answers1

0

It took me few hours to solve my problem, convertation of wmf file to jpg is a bit tricky in Windows. I add the image to temporary excel file, and then download an image from it.

class ExcelHelpers():
    @staticmethod
    def add_img_to_excel(path_to_wmf):
        import xlsxwriter

        workbook = xlsxwriter.Workbook('test.xlsx')
        worksheet = workbook.add_worksheet()

    worksheet.insert_image('A1', path_to_wmf)

    workbook.close()

    @staticmethod
    def get_img_from_excel(long_filename):
        filename = os.path.basename(long_filename).split('.')[0]
        from PIL import ImageGrab
        import win32com.client as win32

        excel = win32.gencache.EnsureDispatch('Excel.Application')
        path_to_excel = os.path.join(os.getcwd(), 'test.xlsx')

        workbook = excel.Workbooks.Open(path_to_excel)

        for sheet in workbook.Worksheets:
            for i, shape in enumerate(sheet.Shapes):
                if shape.Name.startswith('Picture'):
                    shape.Copy()
                    image = ImageGrab.grabclipboard()
                    image.save('{}.jpg'.format(filename), 'jpeg')

        workbook.Close()
        excel.Quit()
        del excel
        os.remove(long_filename)
        os.remove('test.xlsx')
Andrey Mazur
  • 510
  • 4
  • 14