python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

file unreadable (Google Drive) after generating new pptx

Open nhm-7 opened this issue 7 months ago • 0 comments

pptx fails when uploading to Google Drive

Image

After opening the pptx using my local Ppt program (PowerPoint in my case), and "saving as" as new file, it worked. But it does not work directly after running a .py script.

.py Code:

def get_new_ppt(new_images):
    for slide_num, new_imgs in new_images.items():
        slide = prs.slides[slide_num - 1]  # 0-index
        old_images = [shape for shape in slide.shapes if shape.shape_type == 13]  # PICTURE = 13
        if len(old_images) != len(new_imgs):
            print(f"⚠️ Slide {slide_num}: {len(old_images)} old images, {len(new_imgs)} news. Not same lenght.")
            continue
        for old_shape, new_img_path in zip(old_images, new_imgs):
            if not os.path.exists(new_img_path):
                print(f"⚠️ Img not found!, path: {new_img_path}")
                continue
            left = old_shape.left
            top = old_shape.top
            width = old_shape.width
            height = old_shape.height
            sp = old_shape
            slide.shapes._spTree.remove(sp._element)
            slide.shapes.add_picture(new_img_path, left, top, width=width, height=height)
            print(f"✅ Img {new_img_path} has been reeplaced in slide number: {slide_num}")
    return prs

new_images be like:

new_images = {
        5: ["new_image_1.png",
            "new_image_2.png"
           ],
    }

where 5 is the number of slides on which new images will be pasted, using old-images location and shape configs.

and then I save the prs using prs.save("April_test_new_path.pptx")

nhm-7 avatar May 26 '25 17:05 nhm-7