当前位置: 首页 > news >正文

python ppt转pdf以及图片提取

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

  1. 安装
pip instamm PyMuPDF
pip install comtypes
  1. code
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 = 1try:presentation = powerpoint.Presentations.Open(ppt_path, WithWindow=False)presentation.SaveAs(pdf_path, 32)  # 保存为PDFpresentation.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 = 0for 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 += 1doc.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}")continuetry: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)
http://www.dtcms.com/a/564746.html

相关文章:

  • 大连做公司网站的公司用php做网站要用什么软件
  • 中国建设银行网站口公关工资一般多少钱一个月
  • 线性矩阵不等式 (LMI)
  • 基于无六环H校验矩阵和归一化偏移minsum算法的LDPC编译码matlab性能仿真
  • Linux DMA 技术深度解析:从原理到实战
  • PsTools 学习笔记(7.14):PsFile——谁占用了我的文件?一键查清并安全释放
  • 企业级数智化解决方案:行云创新 AI-CloudOS 产品矩阵引领转型价值落地
  • 华为发布Atlas 900 DeepGreen AI服务器:单机柜100PF算力重构AI训练基础设施
  • 线性代数 - 矩阵求逆
  • 什么网站做生鲜比较好企业网站建设注意什么
  • 前端技术栈 ——nvm与Node.js环境安装
  • 具身智能(一)关于VLA模型π0
  • 企业门户网站的意义做淘宝的人就跟做网站一样
  • 私人做网站图片企业网站的建设与维护是什么
  • Enterprise architect工具绘制活动图时如何使用Call Behavior Actions
  • 华为openEuler 22.03 (LTS-SP3) 手动安装单点clickhouse
  • 用docker搭建selenium grid分布式环境
  • App 上架苹果商店全流程详解 从开发者账号申请到开心上架(Appuploader)跨平台免 Mac 上传实战指南
  • SpringBoot中使用tess4j进行OCR(在macos上面开发)
  • Debian开发板3568配置打印机驱动
  • 基于 Debian 服务器的前端项目部署完整教程
  • Debian 服务器环境搭建全指南:从工具选型到项目部署实战
  • app网站如何做推广方案山东进一步优化
  • 长沙网站关键词seo苏州建设工程招投标网
  • 【数据结构】二维数组中的元素查重
  • 软件设计模式(tyutJAVA 状态模式实验)
  • 【LeetCode】最大连续1的个数 III
  • Java 中 组合 (Composition)、接口 (Interface) 和 委托 (Delegation) 这三个概念的区别
  • 日本IT面试:与国内有何不同?一篇解析分享
  • 13-MySQL用户管理