Python应用(四)其他功能(一)python将ppt导出高清图片

忘了哪个链接来着。

WPS高清图片导出需要会员,就为了一个这个小需求开一个会员太亏了,因此就使用python对ppt进行高清图片导出。

将 ppt.SaveAs(imgs_path, 19) 设置format=19即可。

# -*- 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 = '.'
    convert_ppts_to_imgs(path)

 Python应用(四)其他功能(一)python将ppt导出高清图片_第1张图片

 

Python应用(四)其他功能(一)python将ppt导出高清图片_第2张图片

你可能感兴趣的:(工具学习专栏,python,pytorch,keras)