Pptx batch operation slide

Time:2021-11-24

This example uses Python pptx module to batch generate slides, batch modify the content and format of imported slide tables, and insert tables on specified pages

preface

A PRS object is a tree. There are multiple slide slides mounted under it, and there are multiple shape instances under each slide, that is, invisible frames when you make slides. The basic hierarchical relationship is as follows, and table is only a subtype in its shapes collection.

Presentation -> slide_masters -> slide_layouts
Presentation -> slides -> shapes -> placeholders | note | text_frame ...

install

pip install python-pptx
  • rely on
  • Python 2.6, 2.7, 3.3, 3.4, or 3.6
  • lxml
  • Pillow
  • XlsxWriter (to use charting features)

concept

Class or associated property brief introduction correlation method
Presentation Presentation construction object
slide_masters Slide master (a presentation file can have multiple slide masters)
slide_layouts Slide layout (Master, not PRS)
slides Slide object group, pointing to the first page by default add_slide
shapes The shape is similar to the canvas in PS, and each shap (such as table) is equivalent to a layer add_shape
placeholders… Placeholder, dictionary auxiliary class
  • Following equivalence

    prs = Presentation()
    prs.slide_masters[0].slide_layouts[0]
    prs.slide_layouts[0]
  • Hierarchical relationship

    Presentation -> slide_masters -> slide_layouts

    Presentation -> slides -> shapes -> placeholders | note | text_frame ...

Batch creation

Make something out of nothing, see code

from pptx import Presentation

effect

Pptx batch operation slide

Fixed point modification

Generated in the above sectiontest.pptxTo import a template source,

Modify the contents of the second row and column of the first table in slides 2 and 4, and change the original column width

The second table is inserted in the corresponding slide to generate a new modified versionchange.pptx

from pptx import Presentation

effect

The generated effect snapshot is as follows

Pptx batch operation slide

Reasonable deletion

List in PythonlistIs a reference type, the same as JS array. In other words, if an element is deleted by index in the reference object list, the deletion dislocation will occur due to the index reconstruction caused by the deletion operation. Fortunately, the official standard library provides a method based on value deletionremove, whose function signature is as follows

remove(self, value, /)
      Remove first occurrence of value.
      Raises ValueError if the value is not present.

Pptx official does not provide an explicit slide deletion interface, but check it outslidesObject source code, with private properties_sldIdLstPoints to a collection of slide elements. Therefore, deleting a slide essentially deletes an element in the slide set. You only need to maintain a mapping type dictionary, and the page points to the slide instance element. Then remove the instance element of the specified page number by the value remove, as shown below

prs = Presentation("change.pptx")

effect

Pptx batch operation slide

Similarly, if you want to insert several slides into the source slide group, you should also consider using value insertion. The reason is the same as interpolation in the list. Each time a value is inserted, the length pointed to by the original list variable will change again. Indexed with a bit mark, the subscript of the subsequent element of the inserted bit will change due to addition or deletion, rather than pointing to the source value.

crux

shapesObject is a collection class container that can access multiple shape instances under the current page in the form of an array. However, for specific shapes, such as tables, charts, WordArt, and multimedia (audio) resources, you need to assert first and then convert specific class instances for use.

class GraphicFrame(BaseShape):


“””


Container shape for table, chart, smart art, and media objects.


Corresponds to a <p:graphicFrame> element in the shape tree.


“””

supplement

The official API has been able to add, modify and check the basic elements of pptx.

Refer to the official documentation for more functionshttps://python-pptx.readthedocs.io/en/late…

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]