这个简单的操作方法主题侧重于如何在不依赖 MS PowerPoint 的情况下使用 Python **创建 PowerPoint 幻灯片图像。它包括所有必要的配置步骤和一个工作代码示例,用于在 Python 中从 PowerPoint 幻灯片生成图像。此示例应用程序可用于任何配置了 Python 的 .NET Core 支持平台。
使用 Python 创建 PowerPoint 幻灯片图像的步骤
- 设置环境以在您的应用程序中安装 Aspose.Slides for Python 通过 .NET
- 在 Python 文件中导入 aspose.slides 和 aspose.pydrawing
- 使用 Presentation 类对象打开源演示文件以创建幻灯片缩略图
- 加载演示幻灯片集合中的第一张幻灯片
- 为幻灯片缩略图添加用户定义的 X 和 Y 尺寸
- 创建幻灯片缩略图并以 JPG 格式保存在磁盘上
上述步骤指导在 JPG 中编写 Python 代码以将 PowerPoint 幻灯片转换为图像,该过程通过从磁盘加载源演示文稿并访问所需的幻灯片进行渲染来开始。在后续步骤中,将自定义图像尺寸设置为使用 get_thumbnail() 方法呈现幻灯片缩略图并将图像保存在磁盘上。
在 Python 中从 PowerPoint 幻灯片生成图像的代码
import aspose.pydrawing as drawing | |
import aspose.slides as slides | |
# Applying the linence for Aspose.Slides | |
slidesLicense = slides.License() | |
slidesLicense.set_license("Aspose.Total.lic") | |
# Open the source presentation using the Presentation class object | |
with slides.Presentation("NewPresentation.pptx") as samplePres: | |
# Load the first slide inside the presentation slides collection | |
slide = samplePres.slides[0] | |
# Add the user defined X and Y dimensions | |
desiredX = 1200 | |
desiredY = 800 | |
# Calculating the scaled value of X and Y | |
scaleX = (float)(1.0 / samplePres.slide_size.size.width) * desiredX | |
scaleY = (float)(1.0 / samplePres.slide_size.size.height) * desiredY | |
# Create the slide image with set dimensions | |
slideBmp = slide.get_thumbnail(scaleX, scaleY) | |
# Save the image to disk in JPEG format | |
slideBmp.save("Slide_Thumb_out.jpg", drawing.imaging.ImageFormat.jpeg) | |
此处演示了基于 Python* 的*演示图像创建器的开发应用程序代码。 API 还提供了以 TIFF、PNG、BMP 或其他图像格式呈现幻灯片缩略图的功能。图像自定义也可以通过使用 tiff_options、default_regular_font、notes_comments_layouting、大小和使用 get_thumbnail() 方法的不同重载的图像缩放选项等设置来执行。
在此示例中,我们探索了如何使用简单的 API 接口在 Python 中将 PowerPoint 转换为 JPG。如果您想了解如何使用 python 将演示文稿转换为 PDF,请参阅 如何使用 Python 将演示文稿转换为 PDF 上的文章。