文件夹名称和与程序的关系如图所示
PPTs放置ppt或pptx文件,运行程序会自动提取到PDFs
import os
import win32com.client
def convert_ppt_to_pdf(source_directory="PPTs", target_directory="PDFs"):
# 创建目标目录(如果它不存在)
if not os.path.exists(target_directory):
os.makedirs(target_directory)
powerpoint = win32com.client.Dispatch("Powerpoint.Application")
powerpoint.Visible = 1
for filename in os.listdir(source_directory):
try:
input_path = os.path.abspath(os.path.join(source_directory, filename))
output_path = os.path.abspath(os.path.join(target_directory, os.path.splitext(filename)[0] + ".pdf"))
print(f"Attempting to convert: {input_path} to {output_path}") # Print the paths for debugging
deck = powerpoint.Presentations.Open(input_path)
deck.SaveAs(output_path, 32) # 32 represents the file format for PDF in Office Interop
deck.Close()
print(f"Converted {filename} to PDF in {target_directory}.")
except Exception as e:
print(f"Error converting {filename}: {e}")
powerpoint.Quit()
if __name__ == "__main__":
convert_ppt_to_pdf()