1

I'm working with PPTX files in Python, and I need to extract slides grouped by their sections. I'm using the python-pptx library, which works well for general slide manipulation, but it doesn't seem to support sections directly.

So far, I've been able to access individual slides and their content with python-pptx, but I haven’t found any built-in way to get section information (start/end slides for each section). I was hoping there would be an attribute or method in python-pptx to access sections directly, but it seems like this functionality isn’t available.

Ideally, I was expecting python-pptx to provide a way to access sections similarly to how PowerPoint Interop works on Windows. I would like to get a list of sections and then access the slides within each section.

Does anyone know if there's a way to access sections and their slides with python-pptx? If it's not possible, are there any alternative libraries or methods that work in a Linux environment?

3 Answers 3

0

The only thing remotely close of section seems to be "Slide master" . But I don't think it will do the trick.

You can ask for a new feature as described here.

Or even submit the feature yourself in the git repository by following the contributor guide.

Sign up to request clarification or add additional context in comments.

Comments

0

If you were to look in the createSectionsXML function of my md2pptx project (https://github.com/MartinPacker/md2pptx) you'd see that the first slide in a section has a sectionLst element. My code confects it.

This might be useful to you in your question - by navigating the slides and checking their XML.

But this is not a standard part of python-pptx. Except for the slides navigation and XML extraction.

Comments

0

python-pptx has not yet such functionality. But you can access the xml element ./p:ext/p14:sectionLst and get what you need.

The following script shows how to:

from lxml.etree import tostring, Element
from pptx import Presentation

_nsmap = {
    "p14": "http://schemas.microsoft.com/office/powerpoint/2010/main",
    "p": "http://schemas.openxmlformats.org/presentationml/2006/main",
}

def get_slides_by_section_id(presentation):
    e_extLst = presentation.element.xpath("./p:extLst")[0]
    existing_sectionLst = e_extLst.xpath(f"./p:ext/p14:sectionLst", namespaces=_nsmap)
    if not existing_sectionLst:
        raise Exception(f"'./p:ext/p14:sectionLst' not found.")
    e_sections = e_extLst.xpath(f"./p:ext/p14:sectionLst/p14:section", namespaces=_nsmap)
    slides_by_section_id = {}
    for e_section in e_sections:
        section_id = e_section.get("id")
        section_name = e_section.get("name")
        sldIds = e_section.xpath("./p14:sldIdLst/p14:sldId", namespaces=_nsmap)
        # use section_id since section_name might be not unique
        slides_by_section_id[section_id] = {"name": section_name, "slides": []}
        for sldId in sldIds:
            slide = presentation.slides.get(int(sldId.get("id")))
            slides_by_section_id[section_id]["slides"].append(slide)
    return slides_by_section_id

It returns a dictionary wiht the uuid of the section and the name and slides of the section:

{'{296152b3-4585-4c07-8a8b-4464ca467a0a}': {
                                            'name': 'My Section Name',
                                            'slides': [
                                                       <pptx.slide.Slide object>,
                                                      ...
                                                      ]
                                           },
...
}

and call it as follows:

presentation = Presentation("example.pptx")
slides_by_section_id = get_slides_by_section_id(presentation)
for section_id, section_data in slides_by_section_id.items():
    print(f"{section_data['name']}")
    for slide in section_data['slides']:
        # presentation.slides.index is 0-based, add 1 to get the slide's number
        slide_nr = presentation.slides.index(slide) + 1
        print(f"    {slide_nr}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.