python ppt转pdf以及图片提取

采用com唤起 ppt进行转pdf,从pdf中提取图片

  1. 安装
bash 复制代码
pip instamm PyMuPDF
pip install comtypes
  1. code
python 复制代码
import os
import shutil
import comtypes.client
import fitz  
# 逻辑
'''
1. 根据输入的文件夹提取ppt,转成pdf
2. pdf保留和ppt一样的文件夹结构
3. 从pdf提取图片,保留一样的结构,以ppt文件名命名文件夹

'''


def ensure_dir(path):
    if not os.path.exists(path):
        os.makedirs(path)

def ppt_to_pdf(ppt_path, pdf_path):
    powerpoint = comtypes.client.CreateObject("PowerPoint.Application")
    powerpoint.Visible = 1
    try:
        presentation = powerpoint.Presentations.Open(ppt_path, WithWindow=False)
        presentation.SaveAs(pdf_path, 32)  # 保存为PDF
        presentation.Close()
    finally:
        powerpoint.Quit()

# 从pdf中提取图片,保存在文件名目录下
def extract_images_from_pdf(pdf_path, image_dir):
    ensure_dir(image_dir)
    doc = fitz.open(pdf_path)
    img_count = 0
    for page_index in range(len(doc)):
        page = doc.load_page(page_index)
        for img_index, img in enumerate(page.get_images(full=True), start=1):
            xref = img[0]
            base_image = doc.extract_image(xref)
            image_bytes = base_image["image"]
            image_ext = base_image["ext"]
            image_name = f"page{page_index+1:03d}_img{img_index:03d}.{image_ext}"
            image_path = os.path.join(image_dir, image_name)
            with open(image_path, "wb") as f:
                f.write(image_bytes)
            img_count += 1
    doc.close()
    return img_count

# 遍历整个文件夹
def process_ppt_directory(src_root, dst_root,error_dir):
    for root, dirs, files in os.walk(src_root):
        rel_path = os.path.relpath(root, src_root)
        target_dir = os.path.join(dst_root, rel_path)
        error_dir = os.path.join(error_dir, rel_path)
        ensure_dir(target_dir)

        for file in files:
            if file.lower().endswith(('.ppt', '.pptx')):
                ppt_path = os.path.join(root, file)
                base_name = os.path.splitext(file)[0]
                pdf_path = os.path.join(target_dir, base_name + ".pdf")
                image_dir = os.path.join(target_dir, base_name + "_images")
                

                print(f"▶ 转换: {ppt_path}")
                try:
                    ppt_to_pdf(ppt_path, pdf_path)
                    print(f"  ✅ 生成 PDF: {pdf_path}")
                except Exception as e:
                    print(f"❌ PPT {ppt_path} 转 PDF 失败: {e}")
                    ensure_dir(error_dir)
                    shutil.copy2(ppt_path, error_dir)
                    print(f"  ⚠️ 已复制失败文件到: {error_dir}")
                    continue

                try:
                    count = extract_images_from_pdf(pdf_path, image_dir)
                    print(f"  ✅ 提取图片 {count} 张 → {image_dir}")
                except Exception as e:
                    print(f"❌ PDF {pdf_path} 提取图片失败: {e}")
                    ensure_dir(error_dir)
                    shutil.copy2(ppt_path, error_dir)
                    print(f"  ⚠️ 已复制失败文件到: {error_dir}")

if __name__ == "__main__":
    src_dir = r"C:\Users\84977\Desktop\ceshi"   # ppt目录
    dst_dir = r"C:\Users\84977\Desktop\123"   # 输出目录
    error_dir = r"C:\Users\84977\Desktop\345"  # 失败的目录
    process_ppt_directory(src_dir, dst_dir, error_dir)
相关推荐
小陈工15 分钟前
2026年3月31日技术资讯洞察:AI智能体安全、异步编程突破与Python运行时演进
开发语言·jvm·数据库·人工智能·python·安全·oracle
老李的勺子1 小时前
Agent 记忆失效的 5 种方式:完整排查复盘
python·llm
Leo655351 小时前
动态透视报表 + 查询接口 + Excel导出
开发语言·windows·python
清水白石0081 小时前
pytest Fixture 设计实战指南:作用域、依赖链、自动清理与测试资源高效复用
python·pytest
tottoramen2 小时前
如何安装龙虾
python
QC·Rex2 小时前
AI Agent 任务规划实战:从 ReAct 到 Plan-and-Solve 的完整指南
人工智能·python·react
kcuwu.3 小时前
Python面向对象:封装、继承、多态
开发语言·python
YuanDaima20483 小时前
LangChain基础配置与对话模型实战
人工智能·python·langchain·大模型·智能体·langgraph
河西石头3 小时前
分享python项目与开源python项目中的效率法宝--requirements文件的使用
开发语言·python·requirements文件·批量安装python依赖·python虚拟环境配置
不懒不懒3 小时前
【卷积神经网络作业实现人脸的关键点定位功能】
开发语言·python