欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python利用pywin32庫實現(xiàn)將PPT導(dǎo)出為高清圖片

 更新時間:2023年01月29日 15:29:07   作者:虛壞叔叔  
這篇文章主要為大家詳細(xì)介紹了Python如何利用pywin32庫實現(xiàn)將PPT導(dǎo)出為高清圖片的功能,文中的示例代講解詳細(xì),感興趣的小伙伴可以了解一下

一、安裝庫

需要安裝pywin32庫

pip install pywin32

二、代碼原理

WPS高清圖片導(dǎo)出需要會員,就為了一個這個小需求開一個會員太虧了,因此就使用python對ppt進(jìn)行高清圖片導(dǎo)出。

設(shè)置format=19即可:

ppt.SaveAs(imgs_path, 19) 

三、使用效果

輸入一個文件路徑

path = 'D:\\自動化\\課件.pptx'

最后的效果:

會在路徑下創(chuàng)建一個‘’課件‘’文件夾

里面是所有轉(zhuǎn)換后的圖片

四、所有代碼

完整代碼如下

# -*- 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 平臺下轉(zhuǎn)換工具
        借助 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 多進(jìn)程會卡死,后續(xù)優(yōu)化
        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 平臺下轉(zhuǎn)換工具
        借助 office PowerPoint
    '''
    def __init__(self):
        try:
            # 必須以該形式導(dǎo)入 `from win32com import client` 會報錯
            import win32com.client
        except ImportError:
            print('當(dāng)前為windows平臺,缺少 win32com 庫,請執(zhí)行 `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)

到此這篇關(guān)于Python利用pywin32庫實現(xiàn)將PPT導(dǎo)出為高清圖片的文章就介紹到這了,更多相關(guān)Python pywin32 PPT導(dǎo)出為圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Python用三種方式統(tǒng)計詞頻的方法

    詳解Python用三種方式統(tǒng)計詞頻的方法

    這篇文章主要介紹了Python用三種方式統(tǒng)計詞頻,每種方法給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-07-07
  • Python字符串hashlib加密模塊使用案例

    Python字符串hashlib加密模塊使用案例

    這篇文章主要介紹了Python字符串hashlib加密模塊使用案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • python的range和linspace使用詳解

    python的range和linspace使用詳解

    今天小編就為大家分享一篇python的range和linspace使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 使用python繪制二維圖形示例

    使用python繪制二維圖形示例

    今天小編就為大家分享一篇使用python繪制二維圖形示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • python實現(xiàn)單機(jī)五子棋

    python實現(xiàn)單機(jī)五子棋

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)單機(jī)五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • python模擬登錄百度貼吧(百度貼吧登錄)實例

    python模擬登錄百度貼吧(百度貼吧登錄)實例

    python模擬登錄百度貼吧實例分享,大家參考使用吧
    2013-12-12
  • python中使用paramiko模塊并實現(xiàn)遠(yuǎn)程連接服務(wù)器執(zhí)行上傳下載功能

    python中使用paramiko模塊并實現(xiàn)遠(yuǎn)程連接服務(wù)器執(zhí)行上傳下載功能

    paramiko是用python語言寫的一個模塊,遵循SSH2協(xié)議,支持以加密和認(rèn)證的方式,進(jìn)行遠(yuǎn)程服務(wù)器的連接。這篇文章主要介紹了python中使用paramiko模塊并實現(xiàn)遠(yuǎn)程連接服務(wù)器執(zhí)行上傳下載功能,需要的朋友可以參考下
    2020-02-02
  • Python學(xué)習(xí)筆記之常用函數(shù)及說明

    Python學(xué)習(xí)筆記之常用函數(shù)及說明

    俗話說“好記性不如爛筆頭”,老祖宗們幾千年總結(jié)出來的東西還是有些道理的,所以,常用的東西也要記下來,不記不知道,一記嚇一跳,乖乖,函數(shù)咋這么多捏
    2014-05-05
  • python+tifffile之tiff文件讀寫方式

    python+tifffile之tiff文件讀寫方式

    今天小編就為大家分享一篇python+tifffile之tiff文件讀寫方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python3 pandas 操作列表實例詳解

    Python3 pandas 操作列表實例詳解

    這篇文章主要介紹了Python3 pandas 操作列表實例詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09

最新評論