簡體   English   中英

得到一張圖片 - Python-pptx

[英]Get a picture - Python-pptx

我試圖使用python-pptx讀取.pptx文件。 我設法獲得除演示文稿中的圖像之外的所有內容。 下面是我用來識別演示文稿中除文本框架之外的圖像的代碼。 在識別之后我將auto_shape_type作為RECTANGLE (1)但沒有關於圖像。

from pptx import Presentation
from pptx.shapes.picture import Picture

def read_ppt(file):
    prs = Presentation(file)
    for slide_no, slide in enumerate(prs.slides):
        for shape in slide.shapes:
            if not shape.has_text_frame:
                print(shape.auto_shape_type)

理解這個問題的任何幫助表示贊賞。 也歡迎其他選擇。

嘗試查詢shape.shape_type 默認情況下, auto_shape_type像您觀察到的auto_shape_type返回矩形,盡管圖片也可以插入其他形狀並被其他形狀遮罩。

請注意,新插入圖片的默認值為MSO_AUTO_SHAPE_TYPE.RECTANGLE ,它不執行裁剪,因為矩形的范圍與圖片的范圍完全對應。

shape_type應該返回:

標識此形狀類型的唯一整數,在這種情況下無條件地為MSO_SHAPE_TYPE.PICTURE

您可以使用其blob屬性並寫出二進制文件將圖像內容提取到文件:

from pptx import Presentation
pres = Presentation('ppt_image.pptx')
slide = pres.slides[0]
shape = slide.shapes[0]
image = shape.image
blob = image.blob
ext = image.ext
with open(f'image.{ext}', 'wb') as file:
    file.write(blob)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM