You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB
Python

import os
import win32com.client
from config import Config
# 初始化PPT
def init_powerpoint():
powerpoint = win32com.client.Dispatch("PowerPoint.Application")
powerpoint.Visible = 1
return powerpoint
# PPT转png
def ppt2png(pptFileName, downLoad_path, powerpoint):
try:
ppt_path = os.path.abspath(pptFileName)
ppt = powerpoint.Presentations.Open(ppt_path)
# 保存为图片
img_path = os.path.abspath(downLoad_path + ".png")
ppt.SaveAs(img_path, 18) # 18 为 PNG 格式
# 关闭打开的ppt文件
ppt.Close()
except Exception as e:
print(f"PPT转png失败: {pptFileName}")
else:
print("PPT转png成功", pptFileName)
# 批量转换PPT文件
def ppt_to_png(filename, ad_img_path):
powerpoint = init_powerpoint()
if filename.endswith(".ppt") or filename.endswith(".pptx"):
downLoad_path = os.path.join(ad_img_path, os.path.splitext(os.path.basename(filename))[0])
ppt2png(filename, downLoad_path, powerpoint)
powerpoint.Quit() # 退出PowerPoint
# 示例用法
if __name__ == "__main__":
filename = Config.EXPORT_AD_PPT_PATH
# 获取文件的目录
directory = os.path.dirname(filename)
# 定义 adImg 文件夹的路径
ad_img_path = os.path.join(directory, 'adImg')
# 创建输出文件夹
os.makedirs(ad_img_path, exist_ok=True)
ppt_to_png(filename, ad_img_path)