Python利用pywin32库实现将PPT导出为高清图片
目录
- 一、安装库
- 二、代码原理
- 三、使用效果
- 四、所有代码
一、安装库
需要安装pywin32库
pip install pywin32
二、代码原理
wpS高清图片导出需要会员,就为了一个这个小需求开一个会员太亏了,因此就使用python对ppt进行高清图片导出。
设置format=19即可:
ppt.SaveAs(imgs_path, 19)
三、使用效果
输入一个文件路径
path = 'D:\\自动化\\课件.pptx'
最后的效果:
会在路径下创建一个‘’课件‘’文件夹
里面是所有转换后的图片
四、所有代码
完整代码如下
# -*- coding: UTF-8 -*- import os import sys def get_all_ppts(path): # 获取所有 ppt 文件 ppt_paths = [] for root, _, files in os.walk(path): for f in files: suffix = os.path.splitext(f)[-1].lower() if 'ppt' in suffix: ppt_paths.append(os.path.join(root,f)) return ppt_paths class linuxConverter(): ''' Linux 平台下转换工具 借助 libreoffice 和 imagemagick ''' def _run_cmd(self, cmd): try: os.system(cmd) except Exception as e: print('[ERROR] ', e) return False else: returandroidn True def _ppt_to_imgs(self, ppt_path): # ppt - pdf - jpg # libreoffice 多进程会卡死,后续优化 cmd = 'libreoffice --headless --language=zh-CN ' cmd += '--convert-to pdf {}>>/dev/null'.format(ppt_path) success = self._run_cmd(cmd) if not success: print('[ERROR] ppt2pdf: {}'.format(ppt_path)) return success suffix = os.path.splitext(ppt_path)[-1] pdf_path = ppt_path.replace(suffix, 'pdf').split('/')[-1] success, _ = self._pdf_to_imgs(pdf_path) if not success: print('[ERROR] pdf2imgs: {}'.format(ppt_path)) return success def _pdf_to_imgs(self, pdf_path): imgs_folder = os.path.splitext(pdf_path)[0] cmd = 'mkdir {}'.format(imgs_folder) success = self._run_cmd(cmd) if not success: print('[ERROR] mkdir: {}'.format(pdf_path)) return success, '' cmd = 'convert {} {}/幻灯片%d.JPG'.format(pdf_path, imgs_folder) success = self._run_cmd(cmd) return success, imgs_folder def convert(self, ppts_path_list, total_count): error_count = 0 success_count = 0 for idx in range(total_count): ppt_path = ppts_path_list[idx] print('[ {}/{} ] {}'.format(idx+1, total_count, ppt_path)) success, _ = self._ppt_to_imgs(ppt_path) if not success: 开发者_JAV培训error_count += 1 continue js success_count += 1 return error_count, success_count class WinConverter(): ''' Windows 平台下转换工具 借助 office PowerPoint ''' def __init__(self): try: # 必须以该形式导入 `from win32com import client` 会报错 import win32com.client except ImportError: print('当前为windows平台,缺少 win32com 库,请执行 `pip install pywin32` 安装') exit(0) self._ppt_engine = win32com.client.Dispatch('PowerPoint.Application') self._ppt_engine.Visible = True def _ppt2jpg(self, ppt_path, imgs_path): ppt_path = os.path.abspath(ppt_path) imgs_path = os.path.abspath(imgs_path) try: ppt = self._ppt_engine.Presentations.Open(ppt_path) ppt.SaveAs(imgs_path, 18) # 17:jpg, 18:png, 19:bmp p编程客栈pt.Close() except Exception as e: print('[ERROR] ppt2imgs: {}'.format(ppt_path)) return False else: return True def convert(self, ppts_path_list, total_count): error_count = 0 success_count = 0 for idx in range(total_count): ppt_path = ppts_path_list[idx] print('[ {}/{} ] {}'.format(idx+1, total_count, ppt_path)) suffix = os.path.splitext(ppt_path)[-1] imgs_path = ppt_path.replace(suffix,'.png') success = self._ppt2jpg(ppt_path, imgs_path) if not success: error_count += 1 continue success_count += 1 self._ppt_engine.Quit() return error_count, success_count def convert_ppts_to_imgs(path): if os.path.isdir(path): ppts_path_list = get_all_ppts(path) elif os.path.isfile(path): ppts_path_list = [path] if not ppts_path_list: print('该路径下编程未找到 ppt 文件') exit(0) plat = sys.platforjwqQKVIm if 'linux' in plat: converter = LinuxConverter() elif 'win' in plat: converter = WinConverter() total_count = len(ppts_path_list) print('[BEGIN] 共 {} 个 ppt 文件'.format(total_count)) error_count, success_count = converter.convert(ppts_path_list, total_count) print('[END] error:{} success:{}'.format(error_count, success_count)) if __name__ == '__main__': path = 'D:\\自动化\\课件.pptx' convert_ppts_to_imgs(path)
到此这篇关于Python利用pywin32库实现将PPT导出为高清图片的文章就介绍到这了,更多相关Python pywin32 PPT导出为图片内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论