1. 前言
作爲辦公自動化 PPT 系列篇的最後一篇文章,我們將 PPT 中的高級功能及常用點
文章內容將覆蓋:
-
預設形狀 Shape
-
圖表 Chart
-
讀取文字內容
-
保存所有圖片
2. 預設形狀 Shape
實際上,PPT 文檔的內容區就是由各類形狀 Shape 組成,包含:圖片、文本框、視頻、表格、預設形狀
其中,預設的普通形狀也相當豐富,可以查看下面鏈接
使用下面的方法,可以向幻燈片中插入一個形狀
slide.shapes.add_shape(autoshape_type_id, left, top, width, height)
參數分別是:
-
autoshape_type_id 形狀類型
-
left 左邊距
-
top 上邊距
-
width 形狀寬度
-
height 形狀高度
我們以插入一個簡單的圓角矩形框爲例
2-1 插入形狀
from pptx.enum.shapes import MSO_SHAPE, MSO_SHAPE_TYPE
def insert_shape(slide, left, top, width, height, autoshape_type_id=MSO_SHAPE.CHEVRON, unit=Inches):
"""
幻燈片中添加形狀
:param unit: 單位,默認爲Inches
:param autoshape_type_id: 形狀類型
:param slide:幻燈片
:param left:左邊距
:param top:上邊距
:param width:寬度
:param height:高度
:return:
"""
# 添加一個形狀
# add_shape(self, autoshape_type_id, left, top, width, height)
# 參數分別爲:形狀類型、左邊距、上邊距、寬度、高度
shape = slide.shapes.add_shape(autoshape_type_id=autoshape_type_id,
left=unit(left),
top=unit(top),
width=unit(width),
height=unit(height))
return shape
# 1、添加一個圓角矩形
rectangle = insert_shape(slide, 2, 2, 16, 8, autoshape_type_id=MSO_SHAPE.ROUNDED_RECTANGLE, unit=Cm)
2-2 設置形狀屬性
上面方法返回的形狀對象 ,我們可以進一步設置它的背景顏色及邊框屬性
比如:設置背景色爲白色;邊框顏色爲紅色,寬度爲 0.5 釐米
# 2、設置形狀屬性
# 2.1 背景顏色
set_widget_bg(rectangle, bg_rgb_color=[255, 255, 255])
# 2.2 邊框屬性
set_widget_frame(rectangle, frame_rgb_color=[255, 0, 0],frame_width=0.5)
更多形狀可以參考下面鏈接
https://python-pptx.readthedocs.io/en/latest/api/enum/MsoAutoShapeType.html
3. 圖表 Chart
圖表 Chart 是 PPT 中使用很頻繁的一塊內容,使用 python-pptx 可以創建各種類型的圖表,包含:柱狀圖、餅圖、折線圖、散點圖、3D 圖等
創建圖表的方式如下:
slide.shapes.add_shape(autoshape_type_id, left, top, width, height)
參數分別是:
-
autoshape_type_id 圖表樣式
-
left 左邊距
-
top 上邊距
-
width 圖表顯示寬度
-
height 圖表顯示高度
3-1 創建一個折線圖
首先,創建一個圖表數據對象 ChartData
from pptx.chart.data import ChartData
slide = add_slide(self.presentation, 6)
# 創建一個圖表數據對象
chart_data = ChartData()
接着,準備圖表數據
# 數據類別(x軸數據)
chart_data.categories = [2000, 2005, 2010, 2015, 2020]
# 每一年各維度的數據(3個緯度)
# 經濟
chart_data.add_series("經濟", [60, 65, 75, 90, 95])
# 環境
chart_data.add_series("環境", [95, 88, 84, 70, 54])
# 文化
chart_data.add_series("軍事",[40, 65, 80, 95, 98])
最後,指定圖表類型爲折線圖 XL_CHART_TYPE.LINE,按照圖表數據繪製圖表
如果需要繪製其他圖表,可以參考下面鏈接:
https://python-pptx.readthedocs.io/en/latest/api/enum/XlChartType.html
def insert_chart(slide, left, top, width, height, data, unit=Inches, chart_type=XL_CHART_TYPE.COLUMN_CLUSTERED):
"""
插入圖表
:param slide: 幻燈片
:param left: 左邊距
:param top: 上邊距
:param width: 寬度
:param height: 高度
:param data: 圖表數據
:param unit: 數據單位,默認爲:Inches
:param chart_type: 圖表類型,默認是:柱狀圖
:return:
"""
chart_result = slide.shapes.add_chart(chart_type=chart_type,
x=unit(left), y=unit(top),
cx=unit(width), cy=unit(height),
chart_data=data)
# 返回圖表
return chart_result.chart
# 添加圖表
chart = insert_chart(slide, 4, 5, 20, 9, chart_data, unit=Cm, chart_type=XL_CHART_TYPE.LINE)
3-2 設置圖表顯示屬性
以設置圖表圖例、圖表是否顯示平滑、設置圖表文字樣式爲例
# 設置圖表顯示屬性
# 顯示圖例
chart.has_legend = True
# 圖例是否在繪圖區之外顯示
chart.legend.include_in_layout = False
# 設置圖表是否顯示平滑
chart.series[0].smooth = True
chart.series[1].smooth = True
chart.series[2].smooth = True
# 設置圖表中文字的樣式
set_font_style(chart.font, font_size=12, font_color=[255, 0, 0])
最後生成的折線圖效果圖如下:
4. 讀取內容
PPT 文檔的內容區由各種 Shape 組成,並且 shape.has_text_frame 可用於判斷形狀內部是否包含文本框
因此,只需要遍歷所有形狀,就可以獲取 PPT 中所有的文本內容
def read_ppt_content(presentation):
"""
讀取PPT中所有的內容
:param presentation:
:return:
"""
# 所有內容
results = []
# 遍歷所有幻燈片,獲取文本框中的值
for slide in presentation.slides:
for shape in slide.shapes:
# 判斷形狀是否包含文本框
if shape.has_text_frame:
content = get_shape_content(shape)
if content:
results.append(content)
return results
presentation = Presentation("./raw.pptx")
# 1、普通形狀內容的所有文本內容
contents = read_ppt_content(presentation)
print(contents)
但是,對於圖表 Table 單元格中的文本數據,沒法利用這種方式獲取到
我們只能過濾出形狀類型爲 TABLE 的形狀,遍歷表中所有行及單元格,獲取文本數據
def read_ppt_file_table(self):
"""
讀取PPT中的數據
:return:
"""
# 打開待讀取的ppt
presentation = Presentation("./raw.pptx")
for slide in presentation.slides:
# 遍歷素有形狀
# 形狀:有內容的形狀、無內容的形狀
for shape in slide.shapes:
# print('當前形狀名稱:', shape.shape_type)
# 只取表格中的數據,按照行讀取內容
if shape.shape_type == MSO_SHAPE_TYPE.TABLE:
# 獲取表格行(shape.table.rows)
for row in shape.table.rows:
# 某一行所有的單元格(row.cells)
for cell in row.cells:
# 單元格文本框中的內容(cell.text_frame.text)
print(cell.text_frame.text)
5. 保存圖片
有時候,我們需要將 PPT 文檔中的所有圖片保存到本地
只需要下面 3 步即可完成
-
遍歷幻燈片內容區所有形狀
-
過濾出形狀類型爲 MSO_SHAPE_TYPE.PICTURE 的圖片形狀,獲取圖片形狀的二進制字節流
-
將圖片字節流寫入到文件中
def save_ppt_images(presentation, output_path):
"""
保存ppt中所有圖片
[Python批量導出PPT中的圖片素材](https://www.pythonf.cn/read/49552)
:param presentation:
:param output_path 保存目錄
:return:
"""
print('幻燈片數目:', len(presentation.slides))
# 遍歷所有幻燈片
for index_slide, slide in enumerate(presentation.slides):
# 遍歷所有形狀
for index_shape, shape in enumerate(slide.shapes):
# 形狀包含:文字形狀、圖片、普通形狀等
# 過濾出圖片形狀
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
# 獲取圖片二進制字符流
image_data = shape.image.blob
# image/jpeg、image/png等
image_type_pre = shape.image.content_type
# 圖片後綴名
image_suffix = image_type_pre.split('/')[1]
# 創建image文件夾保存抽出圖片
if not os.path.exists(output_path):
os.makedirs(output_path)
# 圖片保存路徑
output_image_path = output_path + random_str(10) + "." + image_suffix
print(output_image_path)
# 寫入到新的文件中
with open(output_image_path, 'wb') as file:
file.write(image_data)
6. 最後
至此,Python 辦公自動化 PPT 系列篇就正式結束了!在實際項目中,如果你有遇到其他問題,歡迎在評論區留言!
我已經將全部源碼上傳到後臺,關注公衆號「 AirPython 」,後臺回覆「 ppt 」即可獲得全部源碼
如果你覺得文章還不錯,請大家 點贊、分享、留言下,因爲這將是我持續輸出更多優質文章的最強動力!
推薦閱讀
最全總結 | 聊聊 Python 辦公自動化之 Excel(上)
最全總結 | 聊聊 Python 辦公自動化之 Excel(中)
最全總結 | 聊聊 Python 辦公自動化之 Excel(下)
最全總結 | 聊聊 Python 辦公自動化之 Word(上)
最全總結 | 聊聊 Python 辦公自動化之 Word(中)
最全總結 | 聊聊 Python 辦公自動化之 Word(下)
最全總結 | 聊聊 Python 辦公自動化之 PPT(上)
最全總結 | 聊聊 Python 辦公自動化之 PPT(中)