PPT转PDF转PNG图片 | Python | comtypes | pdf2image | poppler

业务上需要把PPT转换成PNG图片,使用comtypes先转换成pdf,然后pdf2image加poppler按页转换PNG。

环境加依赖库的准备

1
2
3
4
5
6
7
Python 3.12.4
python -m pip install comtypes
python -m pip install pdf2image
poppler
# 下载地址 https://github.com/oschwartz10612/poppler-windows/releases/
# 安装后配置路径到环境变量Path中,验证工作正常
poppler -v

转换代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import comtypes.client
from pdf2image import convert_from_path
import os

def init_powerpoint():
powerpoint = comtypes.client.CreateObject("Powerpoint.Application", dynamic=True)
powerpoint.Visible = 1
return powerpoint

def ppt_to_pdf(powerpoint, inputFileName, outputFileName, formatType=32):
if outputFileName[-3:] != 'pdf':
outputFileName = outputFileName + ".pdf"
deck = powerpoint.Presentations.Open(inputFileName)
deck.SaveAs(outputFileName, formatType)
deck.Close()

def pdf_to_img(inputFileName):
images = convert_from_path(inputFileName)
for index, image in enumerate(images):
image.save(f'{inputFileName}-{index}.png')

def convert_ppt_to_pdf(powerpoint, folder):
files = os.listdir(folder)
pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
for pptfile in pptfiles:
fullpath = os.path.join(folder, pptfile)
ppt_to_pdf(powerpoint, fullpath, fullpath)

def convert_pdf_to_img(folder):
files = os.listdir(folder)
pdffiles = [f for f in files if f.endswith((".pdf"))]
for pdffile in pdffiles:
fullpath = os.path.join(folder, pdffile)
pdf_to_img(fullpath)

if __name__ == "__main__":
powerpoint = init_powerpoint()
cwd = os.getcwd()
convert_ppt_to_pdf(powerpoint, cwd)
convert_pdf_to_img(cwd)
powerpoint.Quit()

‘POINTER(IUnknown)’ object has no attribute ‘Presentations’错误解决

1
2
3
4
5
6
    deck = powerpoint.Presentations.Open(inputFileName)
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'POINTER(IUnknown)' object has no attribute 'Presentations'

# 初始化powerpoint对象时加上 dynamic=True
powerpoint = comtypes.client.CreateObject("Powerpoint.Application", dynamic=True)

参考文章

batch-ppt-to-pdf
Python实现ppt转pdf代码报错