python如何将多张图片批量插入PPT中?
生活随笔
收集整理的這篇文章主要介紹了
python如何将多张图片批量插入PPT中?
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python如何將多張圖片批量插入PPT中?
- 第一步 獲取圖片
- 第二步 生成PPT
第一步 獲取圖片
批量獲取第一類圖片
import shutil import easygui as g import os# 檢查 ./SourceImage 目錄是否存在,不存在則創(chuàng)建它 if (not (os.path.exists( r"C:\Users\Public\Documents\Word" ))):os.mkdir( r"C:\Users\Public\Documents\Word" ) if (not (os.path.exists( r"C:\Users\Public\Documents\Word1" ))):os.mkdir( r"C:\Users\Public\Documents\Word1" ) if (not (os.path.exists( r"C:\Users\Public\Documents\res" ))):os.mkdir( r"C:\Users\Public\Documents\res" ) path = g.diropenbox() g = os.walk(path) for path, dir_list, file_list in g:for file_name in file_list:if "WholeCoreViewer.png" in os.path.join(path, file_name):picpath2 = os.path.join(path, file_name)print("正在復制... %s " % picpath2)new_name = picpath2.split('\\')new_name2 = new_name[-1]# print(new_name2)shutil.copy(picpath2, os.path.join(r'C:\Users\Public\Documents\Word1', new_name2))批量獲取第二類圖片(第二類圖需要壓縮)
import shutil import easygui as g import base64 import io import os from PIL import Image from PIL import ImageFilecopypath = r'C:\Users\Public\Documents\Word' path = g.diropenbox() g = os.walk(path) a = [] for path, dir_list, file_list in g:for file_name in file_list:if ".JPG" in os.path.join(path, file_name):# print(path)# print( file_list )for i in file_list:if ".JPG" in i:f = ia.append(f)path_name1 =path.replace("\\", "_")path_name2 = path_name1.replace(":", "_")path_name3 =path_name2.replace(". ", "_")# print(os.path.join(path, file_name))picpath = os.path.join(path, file_name)file_name = "\\%s_%s" % (path_name3, a[0])picpath2 = os.path.join(path, copypath + file_name)picpath3 =picpath2.replace(".JPG", ".JPEG")print("正在復制... %s " % picpath3)shutil.copy(picpath, picpath3)# 壓縮圖片文件 def compress_image(outfile, mb=190, quality=85, k=0.9):"""不改變圖片尺寸壓縮到指定大小:param outfile: 壓縮文件保存地址:param mb: 壓縮目標,KB 190kb:param step: 每次調整的壓縮比率:param quality: 初始壓縮比率:return: 壓縮文件地址,壓縮文件大小"""o_size = os.path.getsize( outfile ) // 1024print( "正在壓縮%s圖片,從%skb到%skb" % (picpath3,o_size, mb))if o_size <= mb:print( outfile )print( outfile.split( '.' )[0] + '.png' )return outfileImageFile.LOAD_TRUNCATED_IMAGES = Truewhile o_size > mb:im = Image.open( outfile )x, y = im.sizeout = im.resize( (int( x * k ), int( y * k )), Image.ANTIALIAS )try:out.save( outfile, quality=quality )except Exception as e:print( e )breako_size = os.path.getsize( outfile ) // 1024return outfile# 壓縮base64的圖片 def compress_image_bs4(b64, mb=190, k=0.9):"""不改變圖片尺寸壓縮到指定大小:param outfile: 壓縮文件保存地址:param mb: 壓縮目標,KB:param step: 每次調整的壓縮比率:param quality: 初始壓縮比率:return: 壓縮文件地址,壓縮文件大小"""f = base64.b64decode( b64 )with io.BytesIO( f ) as im:o_size = len( im.getvalue() ) // 1024if o_size <= mb:return b64im_out = imwhile o_size > mb:img = Image.open( im_out )x, y = img.sizeout = img.resize( (int( x * k ), int( y * k )), Image.ANTIALIAS )im_out.close()im_out = io.BytesIO()out.save( im_out, 'jpeg' )o_size = len( im_out.getvalue() ) // 1024b64 = base64.b64encode( im_out.getvalue() )im_out.close()return str( b64, encoding='utf8' )# 將指定文件夾filePath下的 文件地址 和 子文夾下的文件地址 塞進picuture_list列表中 def read_file(filePath):picuture_list = []for dirpath, dirnames, filenames in os.walk( filePath ):path = [os.path.join( dirpath, names ) for names in filenames]picuture_list.extend( path )return picuture_list # jpg格式轉為png格式 def jpg_to_png(file):if file.endswith( 'JPG' ) or file.endswith( 'jpg' ):# 要指明重命名之后的路徑src = os.path.join( copypath, file )r_name = file.split( '.' )[0] + '.png'dct = os.path.join( copypath, r_name )os.rename( src, dct )outfile = read_file( copypath ) for file in outfile:compress_image(file) #先壓縮圖片jpg_to_png( file ) # 再將圖片轉為PNG格式第二步 生成PPT
# 加載庫 from pptx.util import Cm, Inches, Pt from pptx.enum.text import PP_ALIGN from pptx.dml.color import RGBColor import os from pptx import Presentation# 設置路徑work_path = r'C:\Users\Public\Documents\res' os.chdir(work_path)# 實例化 ppt 文檔對象 prs = Presentation() # # 設置幻燈片尺寸,16:9 prs.slide_width = Inches(16) prs.slide_height = Inches(9)# 插入幻燈片 rootdir = r'C:\Users\Public\Documents\Word' rootdir1 = r'C:\Users\Public\Documents\Word1'list = os.listdir(rootdir) list1 = os.listdir(rootdir1) # 列出文件夾下所有的目錄與文件 print(list) # ['噴碳.jpeg', '巖心烘箱.jpeg', '顯微鏡.jpeg'] print(list1) # ['WYD1#_1_1_100cm.WholeCoreViewer.png']# 添加圖片 i = 0 j = 0 while i < len(list1):img_name1 = rootdir + '\\' + list[i]img_name2 = rootdir1 + '\\' + list1[j]print(img_name1)print(img_name2)blank_slide = prs.slide_layouts[6]slide_1 = prs.slides.add_slide( blank_slide )a = slide_1.shapes.add_picture( image_file=img_name1,left=Inches(0.1),top=Inches(2),width=Inches(8.2),height=Inches(5.8))a.rotation = (90.00) # a = slide_1.shapes.add_picture( image_file=r'C:\Users\Public\Documents\Clipboard2.png',left=Inches(8),top=Inches(0),width=Inches(5.5),height=Inches(1))# 以上放置比例標尺的截圖slide_1.shapes.add_picture( image_file=img_name2,left=Inches( 8),top=Inches( 0.8 ),width=Inches( 5.5 ),height=Inches( 8.2) )textbox= slide_1.shapes.add_textbox(left=Inches(0.1),top=Inches(0),width=Inches(6),height=Inches(1))tf = textbox.text_framepara = tf.add_paragraph() # 添加段落para.text = "%s" % list1[i]para.alignment = PP_ALIGN.LEFT # 居中## 設置字體font = para.fontfont.size = Pt(20) # 大小font.name = 'Arial' # 字體font.color.rgb = RGBColor(255, 0, 0) # 紅色i += 1j += 1# 保存 ppt prs.save('result.pptx')總結
以上是生活随笔為你收集整理的python如何将多张图片批量插入PPT中?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信访积案化解和网上信访受理平台实践案例
- 下一篇: 苹果强悍:8.7%的市场份额,75%的行