ppt 转 pdf 功能分离,不默认导入

This commit is contained in:
ktianc 2024-01-05 11:34:21 +08:00
parent 905dccc243
commit e026182838
2 changed files with 40 additions and 45 deletions

View File

@ -0,0 +1,40 @@
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)

View File

@ -6,8 +6,6 @@
# @IDE : PyCharm # @IDE : PyCharm
# @desc : 工具类 # @desc : 工具类
from win32com.client import gencache
import comtypes.client
import datetime import datetime
import random import random
import re import re
@ -103,46 +101,3 @@ async def import_modules_async(modules: list, desc: str, **kwargs):
logger.error(f"AttributeError导入{desc}失败,未找到该模块:{module}") logger.error(f"AttributeError导入{desc}失败,未找到该模块:{module}")
except AttributeError: except AttributeError:
logger.error(f"ModuleNotFoundError导入{desc}失败,未找到该模块下的方法:{module}") logger.error(f"ModuleNotFoundError导入{desc}失败,未找到该模块下的方法:{module}")
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)
if __name__ == '__main__':
# print(generate_invitation_code())
# print(int(datetime.datetime.now().timestamp()))
# print(datetime.datetime.today() + datetime.timedelta(days=7))
print(generate_string(15))