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

Python實現(xiàn)批量將PPT轉換成長圖

 更新時間:2023年08月25日 09:04:58   作者:谷雨之際  
這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)批量將PPT轉換成長圖,并且圖片名稱與PPT文件名稱相同,保存位置相同,感興趣的小伙伴可以了解下

語言:python 3

用法:點擊運行后,彈出窗口,選擇文件夾,程序運行會將文件夾內的所有PPT文件全部轉換成PPT長圖,圖片名稱與PPT文件名稱相同,保存位置相同。

如運行中報錯,需要自行根據(jù)報錯內容按照缺失的庫

共分享兩種代碼,可以嘗試運行。

代碼1

需安裝庫

#安裝庫
pip install pyautogui
#安裝庫
pip install  pillow
 
import os
import comtypes.client
from tkinter import Tk, filedialog
from PIL import Image
def ppt_to_images(ppt_file):
 try:
    # 導入comtypes.client模塊并創(chuàng)建PowerPoint應用程序對象
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    # 設置PowerPoint應用程序為可見狀態(tài),便于觀察操作過程(可選),修改為0后報錯
    #powerpoint.Visible = 1
    # 打開PPT文件,并返回Presentation對象
    presentation = powerpoint.Presentations.Open(ppt_file)
    for i, slide in enumerate(presentation.slides):      #slide是幻燈片序列
        slide.Export(f"slide_{i}.png", "PNG")
    # 關閉PPT文件
    presentation.Close()
    # 退出PowerPoint應用程序
    powerpoint.Quit()
    presentation = None
    print(ppt_file+"分圖完成!")
 except Exception as e:
        print("分圖時發(fā)生錯誤:", str(e))
def merge_images(directory, png_file):
    try:
        Image.MAX_IMAGE_PIXELS = 2 ** 40
        images = []  # 存儲圖片對象
        for file in os.listdir(directory):
            file_path = os.path.join(directory, file)
            if os.path.isfile(file_path) and file.lower().endswith(".png"):
                image = Image.open(file_path)
                images.append(image)
        if len(images) == 0:
            print("未找到PNG格式的圖片文件")
            return None
        max_width = max(image.size[0] for image in images)  # 獲取最大寬度
        total_height = sum(image.size[1] for image in images)  # 計算總高度
        final_image = Image.new("RGBA", (max_width, total_height), (0, 0, 0, 0))  # 創(chuàng)建最終圖像
        # 逐個粘貼圖片到最終圖像中
        y_offset = 0
        for image in images:
            final_image.paste(image, (0, y_offset))
            y_offset += image.size[1]
        final_image.save(png_file)
        print("已生成圖片"+png_file)
        if final_image:
            for file in os.listdir(directory):
                file_path = os.path.join(directory, file)
                if os.path.isfile(file_path) and file.lower().endswith(".png") and "slide" in file:
                    os.remove(file_path)
                    print("已刪除圖片"+file)
    except Exception as e:
        print("合并圖片時發(fā)生錯誤:", str(e))
def select_directory():
    try:
        root = Tk()
        root.withdraw()
        directory = filedialog.askdirectory(title="選擇目錄")
        ppt_files = [f for f in os.listdir(directory) if f.endswith('.pptx')or f.endswith('.ppt')]
        for ppt_file in ppt_files:
          try:
            #print("directory" + directory)
            if ppt_file.lower().endswith(".pptx"):
               png_file = os.path.join(directory, ppt_file[:-5] + ".png")
               ppt_to_images(ppt_file)  # PPT to image
               merge_images(directory, png_file)  # image to images
            elif ppt_file.lower().endswith(".ppt"):
                png_file = os.path.join(directory, ppt_file[:-4] + ".png")
                ppt_to_images(ppt_file)  # PPT to image
                merge_images(directory, png_file)  # image to images
          except Exception as e:
           print("處理PPT文件時發(fā)生錯誤,跳過該文件:", str(e))
        print("轉換完成!")
    except Exception as e:
        print("選擇目錄并轉換PPT文件時發(fā)生錯誤:", str(e))
# 選擇目錄并轉換PPT到PDF格式,再將PDF轉換為長圖
select_directory()
 

代碼2

import os
import comtypes.client
from tkinter import Tk, filedialog
from pptx import Presentation
from PIL import Image
#PPT轉換成圖片
def ppt_to_images(ppt_file, png_file):
    #presentation = powerpoint.Presentations.Open(ppt_file)
    presentation = Presentation(os.path.join(png_file, ppt_file))
    for i, slide in enumerate(presentation.slides):      #slide是幻燈片序列
        slide.export(f"{png_file}/slide_{i}.png")     #將PPT轉換成圖片并保存到目錄下
    print("PPT轉換為圖像完成!")
#將圖片拼接成長圖
def merge_images(ppt_path, output_file):
    images = [Image.open(f"{ppt_path}/{img}") for img in os.listdir(ppt_path) if img.endswith(".png")]
    widths, heights = zip(*(img.size for img in images))
    total_height = sum(heights)
    max_width = max(widths)
    merged_image = Image.new("RGB", (max_width, total_height))
    y_offset = 0
    for img in images:
        merged_image.paste(img, (0, y_offset))
        y_offset += img.size[1]
    merged_image.save(output_file)
    print("圖像拼接完成!")
def ppt_to_pdf(ppt_path, pdf_file):   #ppt路徑和pdf的路徑
    # 導入comtypes.client模塊并創(chuàng)建PowerPoint應用程序對象
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    # 設置PowerPoint應用程序為可見狀態(tài),便于觀察操作過程(可選),修改為0后報錯
    powerpoint.Visible = 1
    # 打開PPT文件,并返回Presentation對象
    presentation = powerpoint.Presentations.Open(ppt_path)
    # 將打開的PPT文件導出為PDF文件(第二個參數(shù)2表示導出為PDF格式)
    presentation.ExportAsFixedFormat(pdf_file, 2)
    # 輸出轉換完成的信息
    print(ppt_path + "轉PDF完成!")
def select_directory():
    root = Tk()
    root.withdraw()
    directory = filedialog.askdirectory(title="選擇目錄")
    ppt_files = [f for f in os.listdir(directory) if f.endswith('.pptx')]
    for ppt_file in ppt_files:
        ppt_path = os.path.join(directory, ppt_file)       #ppt_path ppt的路徑,拼接ppt
        pdf_file = os.path.join(directory, ppt_file[:-4] + ".pdf")    #pdf文件
        png_file= os.path.join(directory, ppt_file[:-4] + ".png")
        ppt_to_pdf(ppt_path, pdf_file)
    print("轉換完成!")
# 選擇目錄并轉換PPT到PDF格式,再將PDF轉換為長圖
select_directory()

到此這篇關于Python實現(xiàn)批量將PPT轉換成長圖的文章就介紹到這了,更多相關Python PPT轉長圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python學習筆記基本數(shù)據(jù)結構之序列類型list tuple range用法分析

    Python學習筆記基本數(shù)據(jù)結構之序列類型list tuple range用法分析

    這篇文章主要介紹了Python學習筆記基本數(shù)據(jù)結構之序列類型list tuple range用法,結合具體實例形式分析了Python序列類型list tuple range基本概念、定義與使用技巧,需要的朋友可以參考下
    2019-06-06
  • Python中搜索和替換文件中的文本的實現(xiàn)(四種)

    Python中搜索和替換文件中的文本的實現(xiàn)(四種)

    本文主要介紹了Python中搜索和替換文件中的文本的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Python并發(fā)編程隊列與多線程最快發(fā)送http請求方式

    Python并發(fā)編程隊列與多線程最快發(fā)送http請求方式

    假如有一個文件,里面有10萬個url,需要對每個url發(fā)送http請求,并打印請求結果的狀態(tài)碼,如何編寫代碼盡可能快的完成這些任務呢
    2021-09-09
  • Ubuntu手動編譯源碼安裝Python的詳細過程

    Ubuntu手動編譯源碼安裝Python的詳細過程

    這篇文章主要介紹了Ubuntu手動編譯源碼安裝Python的詳細過程,在python官網(wǎng)找到所需版本的python安裝包,下載到Ubuntu系統(tǒng)中,需要的朋友可以參考下
    2006-08-08
  • Python基礎之函數(shù)嵌套知識總結

    Python基礎之函數(shù)嵌套知識總結

    今天帶大家回顧python基礎知識,文中對Python函數(shù)嵌套作了非常詳細的知識總結,對正在學習python基礎的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Python 內置方法和屬性詳解

    Python 內置方法和屬性詳解

    這篇文章主要為大家介紹了Python 內置方法和屬性,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 淺談Python pygame繪制機制

    淺談Python pygame繪制機制

    今天給大家?guī)淼氖顷P于Python的相關知識,文章圍繞著Python pygame繪制機制展開,文中有非常詳細的介紹及圖文示例,需要的朋友可以參考下
    2021-06-06
  • python將unicode和str互相轉化的實現(xiàn)

    python將unicode和str互相轉化的實現(xiàn)

    這篇文章主要介紹了python將unicode和str互相轉化的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 打包FlaskAdmin程序時關于static路徑問題的解決

    打包FlaskAdmin程序時關于static路徑問題的解決

    近期寫了個基于Flask-admin的數(shù)據(jù)庫管理程序,通過pyinstaller打包,給別人用,經過幾次嘗試,打包的數(shù)據(jù)一直找不到static里面的樣式文件,查閱資料后,最總把問題搞定了。寫下處理流程,供后來人參考
    2021-09-09
  • Python新手入門最容易犯的錯誤總結

    Python新手入門最容易犯的錯誤總結

    這篇文章主要總結了一些關于Python新手入門最容易犯的錯誤,希望通過學習本文總結的十二點易犯錯誤點,能夠給新手們帶來一定的幫助,需要的朋友可以參考學習,下面來一起看看吧。
    2017-04-04

最新評論