KeyError:“集合中没有类型为‘http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument’的关系”

问题描述 投票:0回答:0
import collections 
import collections.abc
import os
import zipfile
from io import BytesIO
from pptx import Presentation

# Open the PPT file
with open(r'D:\April\0408\test.ppt', 'rb') as f:
    ppt_data = f.read()

    # Unzip the PPT file and read its contents
    with zipfile.ZipFile(BytesIO(ppt_data)) as z:
        for member in z.filelist:
            with z.open(member) as slide:
            
                # Read the PPT file and open Presentation
                if member.filename == 'ppt/presentation.xml':
                    pptx_data = slide.read()

# Change current directory to the PPT file directory since relative path is used inside the PPT file
os.chdir(os.path.dirname(os.path.abspath(r'D:\April\0408\test.ppt')))

# Open the PPT file with the pptx_data that has been extracted above 
ppt = Presentation(BytesIO(pptx_data))

# Set the target folder
target_folder = r"D:\April\0408\temp"

# Iterate through each slide
for index, slide in enumerate(ppt.slides):
    # Iterate through each Shape (the graphic elements within the slide)
    for shape in slide.shapes:
        # Check if the Shape is an image
        if shape.has_image:
            # Get the file name of the image (the file name is saved in the text box under the image)
            picture_name = ""
            for textbox in slide.shapes:
                if textbox.has_text_frame and textbox.text == shape.name:
                    for paragraph in textbox.text_frame.paragraphs:
                        picture_name = paragraph.text
                        break
                    break
            
            # Split the file name and extension
            picture_name, extension = os.path.splitext(picture_name)
            
            # Combine the file name and target folder path to create a new file path
            new_filename = os.path.join(target_folder, f"{picture_name}_{index + 1}{extension}")
            
            # Save the extracted image to the target folder
            with open(new_filename, 'wb') as f:
                f.write(shape.image.blob)

我想用python获取ppt的图片,代码如上,但是返回如下错误:

Traceback (most recent call last):
KeyError: "no relationship of type 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument' in collection"

任何人都可以帮我解决这些问题,我让chatgpt帮忙,但结果反复出现错误,谢谢

python batch-file powerpoint python-pptx
© www.soinside.com 2019 - 2024. All rights reserved.