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: return 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: error_count += 1 continue 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 ppt.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.platform 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導出為圖片內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- python實現MD5進行文件去重的示例代碼
- python 實現百度網盤非會員上傳超過500個文件的方法
- python 實現docx與doc文件的互相轉換
- 詳解Python如何批量檢查圖像是否可用
- Python實現自動化處理Word文檔的方法詳解