1. 前言
上一篇文章簡(jiǎn)單地介紹了 PPT 的文檔結(jié)構(gòu),并使用 python-pptx 這個(gè)依賴庫(kù)完成對(duì) PPT 文檔最基本的操作
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 PPT(上)
作為 PPT 系列篇第 2 篇文章,將覆蓋下列內(nèi)容
表格 Table
圖片 Image,包含靜態(tài)圖片、Gif 動(dòng)態(tài)圖片
視頻 Video
2. 表格 Table
實(shí)例化一個(gè)幻燈片 Slide 對(duì)象后,就可以使用下面的方法插入一個(gè)表格
方法:slide.shapes.add_table(rows,cols,left,top,width,height)
參數(shù)分別是:
rows 表格行數(shù)
cols 表格列數(shù)
left 左邊距
top 上邊距
width 表格寬度
height 表格高度
返回值類型是:pptx.shapes.graphfrm.GraphicFrame
它的 table 屬性即為一個(gè)表格對(duì)象:pptx.table.Table
def insert_table(slide, rows, cols, left, top, width, height, unit=Cm):
"""
幻燈片中插入一個(gè)表格
:param unit: 默認(rèn)單位為厘米
:param slide: 幻燈片對(duì)象
:param rows: 行數(shù)
:param cols: 列數(shù)
:param left: 左邊距
:param top: 上邊距
:param width: 寬度
:param height: 高度
:return:
"""
# 插入一個(gè)表格
table = slide.shapes.add_table(rows, cols, unit(left), unit(top), unit(width), unit(height))
# 返回表格對(duì)象
return table.table
# 1.創(chuàng)建一個(gè)幻燈片 Slide 對(duì)象(空白樣式)
slide = add_slide(self.presentation, 6)
# 2.插入一個(gè)表格
# 參數(shù)分別為:幻燈片對(duì)象、行數(shù)、列數(shù)、左邊距、上邊距、寬度、高度
table = insert_table(slide, 3, 3, 3, 5, 13.6, 5)
2-1 如何重新設(shè)置表的行高、列寬?
為了生成表格的美觀性,對(duì)表的行高、列寬進(jìn)行調(diào)整很有必要
其中,表格對(duì)象的 columns、rows 屬性分別用于獲取所有的列對(duì)象、行對(duì)象
def set_table_column_width(table, column_index, width, unit=Cm):
"""
設(shè)置表格某一列的寬度
:param table:
:param column_index:
:param width:
:param unit: 單位默認(rèn)為厘米
:return:
"""
table.columns[column_index].width = unit(width)
def set_table_row_height(table, row_index, height, unit=Cm):
"""
設(shè)置表格某一行的高度
:param table:
:param row_index:
:param height:
:param unit:
:return:
"""
table.rows[row_index].height = unit(height)
# 3.重新設(shè)置表的寬度、高度
# 3.1 分別設(shè)置第1-3行列寬
set_table_column_width(table, 0, 5)
set_table_column_width(table, 1, 5)
set_table_column_width(table, 2, 5)
# 3.2 分別設(shè)置行高
set_table_row_height(table, 0, 1.5)
set_table_row_height(table, 1, 1.2)
set_table_row_height(table, 2, 1.2)
2-2 設(shè)置單元格數(shù)據(jù)
首先,通過(guò)行索引、列索引獲取對(duì)應(yīng)的單元格對(duì)象
# 獲取某一個(gè)單元格對(duì)象
# 注意:索引從0開始
# 比如:獲取第一行、第一列的單元格對(duì)象
cell = table.cell(0,0)
接著,指定單元格對(duì)象的 text 屬性值為指定的內(nèi)容即可
# 設(shè)置單元格的值
cell.text = "單元格顯示的內(nèi)容"
這樣,我們定義一組數(shù)據(jù),就可以按照插入到表格中了
# 4.設(shè)置表格數(shù)據(jù)
datas = [
["學(xué)員", "姓名", "年齡"],
["", "星安果", 23],
["", "AirPython", 18]]
# 遍歷設(shè)置數(shù)據(jù)到單元格中
for row_index in range(len(table.rows)):
for column_index in range(len(table.columns)):
# 獲取單元格對(duì)象
cell_temp = table.cell(row_index, column_index)
# 設(shè)置數(shù)據(jù)
cell_temp.text = str(datas[row_index][column_index])
2-3 單元格樣式調(diào)整
調(diào)整單元格的樣式包含下面 3 步
獲取單元格文本對(duì)象
拿到文本對(duì)象的段落對(duì)象
通過(guò)段落,指定段落對(duì)齊方式及文字的樣式
以設(shè)置第一行單元格文字加粗、居中顯示為例
# 5、設(shè)置第一行表頭單元格文字加粗居中顯示
for column_index in range(len(table.columns)):
# 1、單元格對(duì)象
cell = table.cell(0, column_index)
# 2、文本控件的段落
paragraph = cell.text_frame.paragraphs[0]
# 3、設(shè)置段落樣式
set_parg_font_style(paragraph, font_name='微軟雅黑', font_size=23, font_color=[255, 0, 0],
font_bold=True)
需要指出的是,單元格中的文本控件除了使用默認(rèn)的段落,也可以添加新的段落,設(shè)置不同的內(nèi)容及樣式
2-4 單元格背景顏色
上一篇文章設(shè)置文本框 TextBox 背景的方法同樣適用于單元格
def set_widget_bg(widget, bg_rgb_color=None):
"""
設(shè)置【文本框textbox/單元格/形狀】的背景顏色
:param widget:文本框textbox、單元格、形狀
:param bg_rgb_color:背景顏色值
:return:
"""
if bg_rgb_color and len(bg_rgb_color) == 3:
# 1、將形狀填充類型設(shè)置為純色
widget.fill.solid()
# 2、設(shè)置文本框的背景顏色
widget.fill.fore_color.rgb = RGBColor(bg_rgb_color[0], bg_rgb_color[1], bg_rgb_color[2])
# 設(shè)置單元格背景顏色
set_widget_bg(cell, [204, 217, 225])
2-5 合并單元格
語(yǔ)法如下:
# 合并單元格
開始單元格.merge(結(jié)束單元格)
以合并單元格并居中顯示為例
from pptx.enum.text import MSO_VERTICAL_ANCHOR, MSO_ANCHOR
def set_cell_center(cell):
"""
設(shè)置單元格文字居中顯示
:param cell:
:return:
"""
paragraph = cell.text_frame.paragraphs[0]
paragraph.alignment = PP_ALIGN.CENTER
cell.vertical_anchor = MSO_ANCHOR.MIDDLE
# 6、單元格合并
# 合并單元格并居中顯示
table.cell(1, 0).merge(table.cell(2, 0))
table.cell(1,0).text="合并"
set_cell_center(table.cell(1,0))
經(jīng)過(guò)上面一系列操作,最后在幻燈片中生成的表格如下:
3. 圖片 Image
無(wú)論是靜態(tài)圖片,或者是 GIF 動(dòng)態(tài)圖片,插入到幻燈片的方式一樣
方法:slide.shapes.add_picture(imge_file,left,top,width,height)
參數(shù)分別為:
image_file 圖片路徑
left 左邊距
top 上邊距
width 圖片顯示寬度
height 圖片顯示高度
def insert_image(slide, pic_path, left, top, width=None, height=None, unit=Inches):
"""
幻燈片中加入圖片(包含靜態(tài)圖片和動(dòng)態(tài)圖片)
:param unit: 單位默認(rèn)為Inches
:param pic_path: 文件路徑
:param slide: 幻燈片對(duì)象
:param left: 左邊距
:param top: 上邊距
:param width: 寬度
:param height: 高度
:return:
"""
# 注意:如果width、height都為None時(shí),以圖片原始大小展示
width = unit(width) if width else None
height = unit(height) if height else None
pic_obj = slide.shapes.add_picture(image_file=pic_path,
left=unit(left),
top=unit(top),
width=width,
height=height)
return pic_obj
?
def image_manage(self):
"""
圖片管理
:return:
"""
# 插入一張靜態(tài)圖片
slide = add_slide(self.presentation, 6)
# 圖片路徑
image_path = './1.jpeg'
# 插入本地圖片
insert_image(slide, image_path, 6, 6, unit=Cm)
需要指出的是,當(dāng) width、height 不顯式指定,默認(rèn)值為 None,則按照?qǐng)D片真實(shí)大小去顯示,當(dāng)圖片很大時(shí),可能會(huì)出現(xiàn)展示不全的情況
因此,在實(shí)際項(xiàng)目中,我們只需要先獲取圖片的寬高比,然后等比例設(shè)置到寬度和高度參數(shù)中即可
from PIL import Image
def get_image_aspect_ratio(image_path):
"""
獲取圖片的寬高比
:param image_path:
:return:
"""
img = Image.open(image_path)
# 圖片類型:GIF
image_format = img.format
# 圖片寬、高
width, height = img.size
# 圖片寬高比
aspect_ratio = width / height
return aspect_ratio
# 獲取寬、高比
aspect_ratio = get_image_aspect_ratio(image_path)
# 等比例插入圖片到PPT中
insert_image(slide, image_path, 6, 6, 6, 6 / aspect_ratio, unit=Cm)
4. 視頻 Video
往 PPT 文檔中插入視頻的方法如下
slide.shapes.add_movie(video_path,left,top,width,height,poster_frame_image)
參數(shù)分別為:
video_path 視頻路徑
left 左邊距
top 上邊距
width 視頻顯示寬度
height 視頻顯示高度
poster_frame_image 視頻封面圖路徑
4-1 獲取視頻寬高比
為了保證視頻在 PPT 中顯示完全,我們需要先獲取視頻的寬、高比
推薦安裝 moviepy 依賴庫(kù),獲取視頻的基本信息
# 安裝依賴
pip3 install moviepy
接著,構(gòu)造一個(gè) VideoFileClip 對(duì)象,從中獲取視頻的寬度、高度
from moviepy.editor import VideoFileClip
def get_video_aspect_ratio_and_thumbnail_path(video_path, frame_index):
"""
獲取圖片的寬、高比
:param video_path: 視頻路徑
:param frame_index 幀索引
:return:
"""
clip = VideoFileClip(video_path)
# 視頻的寬度、高度
width, height = clip.size
# 獲取寬、高比
aspect_ratio = width / height
4-2 獲取視頻幀
視頻封面圖,我們可以從視頻中篩選中一幀,保存到本地
def get_video_frame(clip, frame_index):
"""
獲取視頻的某一幀圖片
:param clip:
:param frame_index:
:return:
"""
# 幀數(shù)目
frame_count = math.floor(clip.fps * clip.duration)
# print('視頻幀數(shù)目:', frame_count)
# 保證參數(shù)輸入有效
if frame_index < 0 or frame_index > frame_count:
frame_index = 1
# 視頻所有的幀
frames = clip.iter_frames()
# clip.get_frame()
# 定義輸出圖片路徑
thumbnail_path = "{}/temp/{}.jpg".format(os.path.abspath(os.path.dirname(__file__)), random_str(10))
# 遍歷,找到對(duì)應(yīng)的幀,保存到本地
for index, frame in enumerate(frames):
if frame_index == index:
# 保持幀圖片到本地
im = Image.fromarray(frame)
im.save(thumbnail_path)
break
return thumbnail_path
4-3 插入視頻
最后,將插入視頻的操作進(jìn)行一次封裝,傳入視頻封面圖、左邊距、上邊距、顯示寬度,即可以完成視頻的插入動(dòng)作
def insert_video(self):
"""
插入視頻
:return:
"""
slide = add_slide(self.presentation, 6)
video_path = './1.mp4'
# 獲取圖片寬高比,并保存一個(gè)臨時(shí)的縮略圖到本地
aspect_ratio, thumbnail_path = get_video_aspect_ratio_and_thumbnail_path(video_path, 120)
# 將視頻插入到PPT中
insert_video(slide, video_path, thumbnail_path, 3, 3, 4, 4 / aspect_ratio)
# 將視頻插入到PPT中
insert_video(slide, video_path, thumbnail_path, 3, 3, 4, 4 / aspect_ratio)
5. 最后
本篇文章講到了 PPT 文檔中關(guān)于表格、圖片、視頻這 3 種常見內(nèi)容的操作
我已經(jīng)將全部源碼上傳到后臺(tái),關(guān)注公眾號(hào)「 AirPython 」,后臺(tái)回復(fù)「 ppt 」即可獲得全部源碼
如果你覺得文章還不錯(cuò),請(qǐng)大家 點(diǎn)贊、分享、留言下,因?yàn)檫@將是我持續(xù)輸出更多優(yōu)質(zhì)文章的最強(qiáng)動(dòng)力!
推薦閱讀
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 Excel(上)
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 Excel(中)
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 Excel(下)
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 Word(上)
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 Word(中)
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 Word(下)
最全總結(jié) | 聊聊 Python 辦公自動(dòng)化之 PPT(上)