【问题标题】:Download WMF from pptx and decode to JPEG从 pptx 下载 WMF 并解码为 JPEG
【发布时间】:2019-10-15 21:59:43
【问题描述】:

你好 stackoverflow 用户。

我正在尝试从 powerpoint 演示文稿中下载图像,然后对其进行处理(以识别其上特定坐标处的数字)。

我的问题是我只能从 pptx 数据中下载 .wmf 格式的图像,并且无法转换。我已经尝试了所有可能的解决方案。

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

有趣的是,在 Powerpoint 中,我可以在保存文件时选择所需的 .jpeg 扩展名。

【问题讨论】:

  • @PankajJoshi 这个答案不起作用,当我手动更改扩展名时,我无法再打开它了
  • ppt可以分享吗?
  • 很遗憾没有,但我找到了解决方法

标签: python opencv python-imaging-library


【解决方案1】:

我花了几个小时才解决我的问题,在 Windows 中将 wmf 文件转换为 jpg 有点棘手。我将图像添加到临时 excel 文件中,然后从中下载图像。

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')

【讨论】:

    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 2017-02-11
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多