import
os
import
win32com.client
from
PIL
import
Image
print
(
"本工具20230620发布于吾爱。qzwsa"
)
def
ppt_to_images():
ppt_files
=
[file_name
for
file_name
in
os.listdir()
if
file_name.endswith(
".ppt"
)
or
file_name.endswith(
".pptx"
)]
if
len
(ppt_files)
=
=
0
:
print
(
"没有找到 PPT 文件!"
)
return
[]
app
=
win32com.client.Dispatch(
"PowerPoint.Application"
)
result
=
{
"ppt_count"
:
0
,
"image_count"
:
0
,
"merged_count"
:
0
}
for
file_name
in
ppt_files:
result[
"ppt_count"
]
+
=
1
print
(f
"正在处理第 {result['ppt_count']} 个 PPT 文件:{file_name}..."
)
presentation
=
app.Presentations.
Open
(os.path.abspath(file_name))
image_list
=
[]
for
i, slide
in
enumerate
(presentation.Slides):
image_path
=
f
"{os.path.splitext(file_name)[0]}_{i+1}.jpg"
image_path
=
os.path.join(os.getcwd(), image_path)
slide.Export(image_path,
"JPG"
)
result[
"image_count"
]
+
=
1
image
=
Image.
open
(image_path)
image_list.append(image)
presentation.Close()
if
len
(image_list)
=
=
0
:
continue
width, height
=
image_list[
0
].size
height
*
=
len
(image_list)
merged_image
=
Image.new(
"RGB"
, (width, height))
for
i, image
in
enumerate
(image_list):
merged_image.paste(image, (
0
, i
*
height
/
/
len
(image_list)))
ppt_name
=
os.path.splitext(file_name)[
0
]
merged_image_path
=
os.path.join(os.getcwd(), f
"{ppt_name}-合并图.jpg"
)
merged_image.save(merged_image_path)
result[
"merged_count"
]
+
=
1
app.Quit()
if
result[
"ppt_count"
]
=
=
0
:
return
[]
print
(f
"已处理 {result['ppt_count']} 个 PPT,共生成 {result['image_count']} 张图片,合并了 {result['merged_count']} 张大图。"
)
return
[f
"{os.path.splitext(file_name)[0]}-合并图.jpg"
for
file_name
in
ppt_files
if
len
([slide
for
slide
in
win32com.client.Dispatch(
"PowerPoint.Application"
).Presentations.
Open
(os.path.abspath(file_name)).Slides]) >
0
]
if
__name__
=
=
"__main__"
:
ppt_to_images()