在本教程中,我们将重点介绍如何在不依赖 PowerPoint 的情况下使用 Python 将 SVG 转换为 Presentation。该应用程序可以在 Windows、Linux 或 macOS 等操作系统中的任何 .NET 核心和 Python 配置环境中使用,以将 SVG 转换为 Python 中的 PPTX**。
在 Python 中将 SVG 导出到 PPTX 的步骤
- 在您的应用程序中将环境配置为 通过 .NET 使用 Aspose.Slides for Python 以将 SVG 转换为演示文稿
- 使用 Presentation 类的实例创建默认演示文稿
- 从演示文稿的幻灯片集合中加载第一张幻灯片
- 打开 SVG 文件,将其内容作为字符串读取并将其插入到演示图像集合中
- 使用 IPictureFrame 实例在添加了 SVG 图像的幻灯片中插入相框
- 将带有 SVG 图像的 PPTX 文件保存在磁盘上
Python 中的上述简单步骤使用简单的 API 接口将 SVG 导出为 PPT 演示文稿。我们将首先使用 Presentation 类的实例创建默认演示文稿,并从演示文稿的幻灯片集合中访问第一张默认幻灯片。然后我们将从磁盘加载和读取 SVG 文件内容作为字符串,并将其添加到演示图像集合中的 IPPImage 中。最后,通过使用 IPictureFrame 类实例,将添加一个相框形状,该形状将在将生成的演示文稿保存在磁盘上之前利用添加的 SVG 文件。
在 Python 中将 SVG 转换为 PPTX 的代码
import aspose.slides as slides | |
filepath = "C://Words//" | |
#Applying the licence for Aspose.Slides to convert SVG to PPTX | |
svgtoSlidesLicense = slides.License() | |
svgtoSlidesLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic") | |
# Make an empty presentation using the Presentation class object | |
with slides.Presentation() as sampleSvgPres: | |
#Access the first slide of the newly created presentation | |
slideForSvg = sampleSvgPres.slides[0] | |
#Load the SVG file content and insert that inside the presentation image collection | |
with open(filepath + "410.svg", 'r') as svgfile: | |
svgContent = svgfile.read().rstrip() | |
ppSVGImage = slides.SvgImage(svgContent) | |
#Add an SVG Image from the disk inside the images collection of the presentation | |
svgImageForSlide = sampleSvgPres.images.add_image(ppSVGImage) | |
#Insert a picture frame inside the shapes collection of the slide | |
slideForSvg.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 0, 0, 720, 540, svgImageForSlide) | |
#Save the presentation in PPTX format with an SVG image on the disk | |
slideForSvg.save(filepath + "PresentationWithSvg.pptx", slides.export.SaveFormat.PPTX) | |
print("Process Completed") | |
本主题解释了如何使用 Python 在演示文稿中插入 SVG。如果您有兴趣了解如何在 PowerPoint 幻灯片中插入表格,请参阅 如何使用 Python 在 PowerPoint 中创建表格 上的文章。