aicheckv2-api/utils/ppt_to_pdf.py
2025-04-11 08:54:28 +08:00

41 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from win32com.client import gencache
import comtypes.client
from core.logger import logger
def ppt_to_pdf_1(ppt_path: str, pdf_path: str):
"""
ppt 转 pdf会弹出 office 软件
:param ppt_path:
:param pdf_path:
:return:
"""
# 创建PDF
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
slide = powerpoint.Presentations.Open(ppt_path)
# 保存PDF
slide.SaveAs(pdf_path, 32)
slide.Close()
# 退出 office 软件
powerpoint.Quit()
def ppt_to_pdf_2(ppt_path: str, pdf_path: str):
"""
完美办法PPT 转 PDF
:param ppt_path:
:param pdf_path:
:return:
"""
p = gencache.EnsureDispatch("PowerPoint.Application")
try:
ppt = p.Presentations.Open(ppt_path, False, False, False)
ppt.ExportAsFixedFormat(pdf_path, 2, PrintRange=None)
ppt.Close()
p.Quit()
except Exception as e:
print(os.path.split(ppt_path)[1], "转化失败,失败原因%s" % e)
logger.info(os.path.split(ppt_path)[1], "转化失败,失败原因%s" % e)