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)

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能之心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值