【问题标题】:Saving area within a shape (rectangle) as image from multiple powerpoint slides将形状(矩形)内的区域保存为来自多个 powerpoint 幻灯片的图像
【发布时间】:2022-05-23 22:07:49
【问题描述】:

我正在尝试抓取在幻灯片幻灯片上的矩形内构建的图表的图像。我找到了 python-pptx 并且能够识别每张幻灯片上的形状。有什么方法可以扩展它以拍摄矩形内区域的快照并将其导出为图像?

# Auto grab the photos created in Powerpoint

from pptx import Presentation

prs = Presentation('ex.pptx')

for slide in prs.slides:
    print(slide)
        for shape in slide.shapes:
            print(shape)
# Identify shape on each slide, find area within, and save as .png

【问题讨论】:

  • 它需要在什么平台上运行?
  • 这只需要在 Windows 10 机器上运行

标签: python powerpoint python-pptx


【解决方案1】:

我认为你最好看看 COM32 类型的解决方案,或者在 VBA 中编写一些东西,或者如果你真的想要 Python 解决方案,可能在 Python 中使用 win32com 库。

无论哪种方式,这都会启动一个“实时”的 PowerPoint 应用程序实例并基本上通过远程控制运行它。这种事情在服务器端不是一个好主意,但如果只是为了个人生产力,它可能会很好。

python-pptx 不能做这种事情,而且可能永远也不会。渲染引擎需要参与此类工作,python-pptx 严格来说是.pptx 文件编辑器/生成器。

【讨论】:

    【解决方案2】:

    使用Aspose.Slides for Python,您可以轻松地将演示文稿形状保存到图像中。以下代码示例向您展示了如何将演示文稿中的所有图表保存为 PNG 图像:

    import aspose.slides as slides
    import aspose.slides.charts as charts
    import aspose.pydrawing as draw
    
    with slides.Presentation("example.pptx") as presentation:
        for slide_index, slide in enumerate(presentation.slides):
            for shape_index, shape in enumerate(slide.shapes):
                # Looking for charts, for example.
                if isinstance(shape, charts.Chart):
                    # Get a chart image.
                    with shape.get_thumbnail() as chart_image:
                        # Save the chart image to PNG.
                        image_path = "chart_image_{}_{}.png".format(slide_index, shape_index)
                        chart_image.save(image_path, draw.imaging.ImageFormat.png)
    

    Aspose.Slides for Python 是一款付费产品,但您可以获得临时许可证或在试用模式下使用它来评估管理演示文稿的所有功能。或者,您可以使用Aspose.Slides Cloud SDK for Python。这个包还提供了一个基于 REST 的 API 来管理演示。下面的代码示例向您展示了如何使用 Aspose.Slides Cloud 来做同样的事情:

    import asposeslidescloud
    import aspose.pydrawing as draw
    
    from asposeslidescloud.apis.slides_api import SlidesApi
    from asposeslidescloud.models import *
    
    slides_api = SlidesApi(None, "my_client_id", "my_client_secret")
    
    file_name = "example.pptx"
    
    # Upload the presentation to the default storage.
    with open(file_name, "rb") as file_stream:
        slides_api.upload_file(file_name, file_stream)
    
    # Get the number of slides.
    slides_info = slides_api.get_slides(file_name)
    slide_count = len(slides_info.slide_list)
    
    for slide_index in range(1, slide_count + 1):
        # Get the number of shapes on the current slide.
        shapes_info = slides_api.get_shapes(file_name, slide_index)
        shape_count = len(shapes_info.shapes_links)
    
        for shape_index in range(1, shape_count + 1):
            shape = slides_api.get_shape(file_name, slide_index, shape_index)
    
            # Looking for charts, for example.
            if shape.type == "Chart":
                # Get the chart as a PNG image.
                image_path = slides_api.download_shape(file_name, slide_index, shape_index, ShapeExportFormat.PNG)
                print("A chart image was saved to " + image_path)
    

    这也是一种付费产品,但您每月可以出于任何目的进行 150 次免费 API 调用。

    我在 Aspose 担任支持开发人员,可以在 Aspose.Slides forum 上回答您关于这些库的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多