【ppt2svg svg2png/jpg】ppt转图片解决方案
pptx处理工具
- pptx 免费,功能有限,会有一些问题
- aspose 收费,功能丰富,有水印
- spire 收费,功能丰富,有水印
- 基于js/vue的方案
PPT转图片
为了跳过水印,有几种间接的方案
方案一
- ppt -> svg
- svg -> png/jpg
linux
apt-get update
apt-get install -y --no-install-recommends libgdiplus libc6-dev
pip install aspose.slides
pip install CairoSVG # 如果缺失字体,需要进行安装
windows
pip install aspose.slides
pip install CairoSVG
import io
import cairosvg
import aspose.slides as aspose_slides
presentation = aspose_slides.Presentation(pptx_path)
for i, slide in enumerate(presentation.slides):
buffer = io.BytesIO()
slide.write_as_svg(buffer)
buffer.seek(0)
svg_data = buffer.read().decode("utf-8")
...(删除水印)
cairosvg.svg2png(
bytestring=svg_data.encode("utf-8"), write_to=f'test/{i}.png', scale=4)
方案二
- pptx -> pdf
- pdf -> png/jpg
linux
sudo apt update && sudo apt upgrade
sudo apt-get install libreoffice
libreoffice --headless --convert-to pdf /path/to/ppt
OR
apt update && apt install python3-uno unoconv
sed -i 's|#!/usr/bin/env python3|#!/usr/bin/python3|' /usr/bin/unoconv
unoconv -f pdf /path/to/ppt
THEN
pip install PyMuPDF
import fitz
pdf_path = ''
pdfDoc = fitz.open(pdf_path)
for i, page in enumerate(pdfDoc.pages()):
rotate = int(0)
# 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。
# 此处若是不做设置,默认图片大小为:792X612, dpi=72
# pix = page.get_pixmap()
zoom_x = 6
zoom_y = 6
# (1.33333333-->1056x816) (2-->1584x1224) (3-->3572x2526)
# x和y的值越大越清晰,图片越大,但处理也越耗时间,这里取决于你想要图片的清晰度
# 默认为1.333333,一般日常使用3就够了,不能设置太大,太大容易使电脑死机
mat = fitz.Matrix(zoom_x, zoom_y)
pix = page.get_pixmap(matrix=mat, dpi=None, colorspace='rgb', alpha=False)
pix.save(f'images/{i}.png')
OR
sudo apt-get install imagemagick
convert -density 250 -quality 100 tmp-good.pdf slide-%d.jpg
win
安装libreoffice
...
方案三
只能win系统,使用PowePoint/WPS接口
pip install comtypes
import os
from comtypes.client import CreateObject
#
初始化PowerPoint应用
powerpoint = CreateObject("Powerpoint.Application") # KWPP.Application
powerpoint.Visible = 1
# 打开PPT文件
pptx_path = os.path.abspath(pptx_path)
ppt = powerpoint.Presentations.Open(pptx_path)
# 遍历每个幻灯片并保存为图片
save_path = 'slides'
fori, slide in enumerate(ppt.Slides): # 0->
image_path = f"{save_path}/slide_{i}.png"
image_path = os.path.abspath(image_path)
slide.Export(image_path, "PNG")
# 关闭PPT文件和PowerPoint应用
ppt.Close()
powerpoint.Quit()
方案四
- 使用linux-WPS