错误提示:_ctypes.COMError:+(-2147467259,+'未指定的错误',+('Presentation+(unknown+member)+:+PowerPoint+无法将+^0+保存到+^1。',+'Microsoft+PowerPoint',+'',+0,+None))
源代码:# 使用此指令前,请确保安装必要的Python库,例如使用以下命令安装:
# pip install comtypes
# pip install tkinter
import os
import comtypes.client
from tkinter import Tk, filedialog
from typing import *
from xbot importprint
defconvert_ppt_folder_to_pdf():
"""
title: 从指定文件夹中逐个导出所有PPT文件为PDF文件
description: 用户可在前台手动选择文件夹,将该文件夹中的所有PPT文件逐个导出为PDF文件,并返回导出为PDF文件的PPT文件列表
inputs:
- 无需输入参数,用户在前台手动选择文件夹
outputs:
- pdf_files (list): 导出为PDF文件的PPT文件列表,eg: ["D:/PPT_Files/example1.pdf", "D:/PPT_Files/example2.pdf"]
"""
# 创建Tkinter根窗口并隐藏
root = Tk()
root.withdraw()
# 打开文件夹选择对话框
folder_path = filedialog.askdirectory(title="选择包含PPT文件的文件夹")
ifnot folder_path:
return []
# 创建PowerPoint应用程序对象
powerpoint = comtypes.client.CreateObject("KWPP.Application")
powerpoint.Visible = 1
pdf_files = []
# 遍历文件夹中的所有文件
for file_name in os.listdir(folder_path):
if file_name.endswith(".ppt") or file_name.endswith(".pptx"):
ppt_path = os.path.join(folder_path, file_name)
pdf_path = os.path.splitext(ppt_path)[0] + ".pdf"
# 打开PPT文件
presentation = powerpoint.Presentations.Open(ppt_path)
# 将PPT导出为PDF
presentation.SaveAs(pdf_path, 32) # 32表示PDF格式
# 关闭PPT文件
presentation.Close()
pdf_files.append(pdf_path)
# 退出PowerPoint应用程序
powerpoint.Quit()
return pdf_files