# -- coding: utf-8 --
from pdf2image import convert_from_path
from pptx import Presentation
from pptx.util import Inches

def pdf_to_ppt(pdf_path, ppt_path):
    # 打开 PDF 文件并将其转换为图像列表
    images = convert_from_path(pdf_path)

    # 创建一个新的 PPT 演示文稿
    prs = Presentation()

    # 遍历每个图像
    for image in images:
        # 创建一个空白幻灯片
        slide = prs.slides.add_slide(prs.slide_layouts[6])

        # 保存图像到临时文件
        image_path = "temp_image.jpg"
        image.save(image_path, 'JPEG')

        # 获取幻灯片的宽度和高度
        slide_width = prs.slide_width
        slide_height = prs.slide_height

        # 插入图像到幻灯片中,并调整大小以适应幻灯片
        pic = slide.shapes.add_picture(image_path, 0, 0, width=slide_width, height=slide_height)

    # 保存 PPT 文件
    prs.save(ppt_path)

    print(f"PDF 文件 {pdf_path} 已成功转换为 PPT 文件 {ppt_path}")

# 调用函数进行转换
pdf_path = r"C:\Users\32717\Documents\WeChat Files\wxid_o66fzv6tiktx22\FileStorage\File\2025-03\学子心志愿行.pdf"
ppt_path = r"C:\Users\32717\Documents\WeChat Files\wxid_o66fzv6tiktx22\FileStorage\File\2025-03\学子心志愿行.pptx"
pdf_to_ppt(pdf_path, ppt_path)
Logo

中国智能体开发者社区,聚焦智能体与大模型开发,提供前沿资讯、实用工具链、开源项目及行业案例。通过技术沙龙、开发者大赛等活动,促进经验交流与协作,助力开发者快速构建创新智能应用。

更多推荐